2016-05-19 69 views
1

我正在開發一款應用程序,其中我需要從SD卡中選擇圖像並將其發送到IBM Waston Visual Recognition服務以識別圖像中的內容。我做這樣的..將圖像URI轉換爲File對象

... 
private VisualRecognition service; 
private VisualClassification result; 
... 

private void openImageFromSDCard(){ 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_FILE); 
} 

private void callIBMWatsonVisualRecognition(){ 

    try { 
     ContentResolver cr = this.getContentResolver(); 
     InputStream is = cr.openInputStream(image); 
     File file = new File("Image.jpg"); 
     FileOutputStream fr = new FileOutputStream(file); 
     int c; 
     while ((c = is.read()) != -1) { 
      fr.write(c); 
     } 
     result = service.classify(file).execute(); 
     tvResult.setText(result.toString()); 
     is.close(); 
     fr.close(); 
    }catch (Exception e){ 
     Log.d("THINK", "Error = " + e); 
    } 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 

     if (requestCode == SELECT_FILE) { 

      image = data.getData(); 
      tvResult.setText(image.toString()); 
      imageSelectFlag = true; 

     } 
    } 
} 
... 

錯誤:

java.lang.IllegalArgumentException: image cannot be null or not be found 
     at com.ibm.watson.developer_cloud.util.Validator.isTrue(Validator.java:38) 
     at com.ibm.watson.developer_cloud.visual_recognition.v2_beta.VisualRecognition.classify(VisualRecognition.java:152) 
     at com.ibm.watson.developer_cloud.visual_recognition.v2_beta.VisualRecognition.classify(VisualRecognition.java:124) 
     at com.algor7.watsonvisiondemo.MainActivity.callIBMWatsonVisualRecognition(MainActivity.java:75) 
     at com.algor7.watsonvisiondemo.MainActivity.onClick(MainActivity.java:57) 
     at android.view.View.performClick(View.java:5204) 
     at android.view.View$PerformClick.run(View.java:21153) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

請檢查VisualRecognition和VisualClassification類(V2)here

回答

3
File file = new File(image.toString()); 

這不是你如何consume content identified by a Uri。或者:

  • 使用ContentResolveropenInputStream(),然後通過InputStream到庫中,或

  • 使用ContentResolveropenInputStream(),然後使用Java I/O來複制InputStream某些文件,您控制,然後傳遞File到你的庫

+0

根據你的建議我這樣做這樣的.. – Algor7

+0

嘗試{ ContentResolver cr = this.getContentResolver(); InputStream is = cr.openInputStream(image); File file = new File(「Image.jpg」); FileOutputStream fr = new FileOutputStream(file); int c; ((c = is.read())!= -1)fr.write(c); } result = service.classify(file).execute(); tvResult.setText(result.toString()); is.close(); fr.close(); (例如e){ Log.d(「THINK」,「Error =」+ e); } – Algor7

+0

但是得到「java.io.FileNotFoundException:Image.jpg:打開失敗:EROFS(只讀文件系統)」。當然,我做錯了什麼。請糾正我。 – Algor7