2017-02-02 7 views
0

私は、SDカードから画像を選択し、IBM Waston Visual Recognitionサービスに送信して画像内のコンテンツを識別する必要があるアプリを開発しています。私は...このようAndroidでのワトソンの視覚認識

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)  

答えて

2

あなたがメイン(UI)からサーバーをヒットしているスレッドではありませんアンドロイドで許可されています。個別のスレッドまたは非同期タスクでanalizarImagen()メソッドを呼び出します。 https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

+1

おかげで、それは完璧に動作します:この文書は

チェック –

関連する問題