简体   繁体   English

Android中的Watson视觉识别

[英]Watson Visual Recognition in Android

I am developing an app in which my requirement to select an image from the SD card and send in to IBM Waston Visual Recognition service to identify the content in the image. 我正在开发一个应用程序,其中需要从SD卡中选择一个图像并发送到IBM Waston Visual Recognition服务以识别图像中的内容。 I am doing like this.. 我就是这样

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RESULT_LOAD_IMAGE);
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RESULT_LOAD_IMAGE && resultCode == MainActivity.this.RESULT_OK && null != data){
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = MainActivity.this.getContentResolver().query(selectedImage,filePathColumn,null,null,null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        analizarImagen(picturePath);
    }
}

private void analizarImagen(String path){
    File image = new File(path);
    System.out.println(path);

    VisualRecognition service = new VisualRecognition(VisualRecognition.VERSION_DATE_2016_05_20);
    service.setApiKey("api-key");

    ClassifyImagesOptions options = new ClassifyImagesOptions.Builder()
            .images(image)
            .build();
    VisualClassification result = service.classify(options).execute();
    System.out.println(result.toString());

}

But when I select an image, the application crashes 但是当我选择图像时,应用程序崩溃

Error: 错误:

02-02 03:43:49.720 3355-3355/? E/AndroidRuntime: FATAL EXCEPTION: main
                                         java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/23 }} to activity {com.cucea.mauricio.visual/com.cucea.mauricio.visual.MainActivity}: android.os.NetworkOnMainThreadException
                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
                                             at android.app.ActivityThread.access$1100(ActivityThread.java:130)
                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                             at android.os.Looper.loop(Looper.java:137)
                                             at android.app.ActivityThread.main(ActivityThread.java:4745)
                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                             at dalvik.system.NativeStart.main(Native Method)
                                          Caused by: android.os.NetworkOnMainThreadException
                                             at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
                                             at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
                                             at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
                                             at java.net.InetAddress.getAllByName(InetAddress.java:214)
                                             at okhttp3.Dns$1.lookup(Dns.java:39)
                                             at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
                                             at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
                                             at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
                                             at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
                                             at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
                                             at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
                                             at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
                                             at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                             at okhttp3.RealCall.getResponse(RealCall.java:243)
                                             at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
                                             at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
                                             at okhttp3.RealCall.execute(RealCall.java:57)
                                             at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:179)
                                             at com.cucea.mauricio.visual.MainActivity.analizarImagen(MainActivity.java:84)
                                             at com.cucea.mauricio.visual.MainActivity.onActivityResult(MainActivity.java:68)
                                             at android.app.Activity.dispatchActivityResult(Activity.java:5192)
                                             at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
                                             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184) 
                                             at android.app.ActivityThread.access$1100(ActivityThread.java:130) 
                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                             at android.os.Looper.loop(Looper.java:137) 
                                             at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                             at dalvik.system.NativeStart.main(Native Method) 

You are hitting server from main(UI) thread which is not allowed in android. 您是通过android中不允许的main(UI)线程访问服务器。 Call your analizarImagen() method in separate thread or async task. 在单独的线程或异步任务中调用analizarImagen()方法。

Check this document: https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html 检查此文档: https : //developer.android.com/reference/android/os/NetworkOnMainThreadException.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM