簡體   English   中英

Android中的Watson視覺識別

[英]Watson Visual Recognition in Android

我正在開發一個應用程序,其中需要從SD卡中選擇一個圖像並發送到IBM Waston Visual Recognition服務以識別圖像中的內容。 我就是這樣

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());

}

但是當我選擇圖像時,應用程序崩潰

錯誤:

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) 

您是通過android中不允許的main(UI)線程訪問服務器。 在單獨的線程或異步任務中調用analizarImagen()方法。

檢查此文檔: https : //developer.android.com/reference/android/os/NetworkOnMainThreadException.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM