2014-01-22 107 views
0

我試圖獲取URI的圖像位圖,但它給了我以下錯誤。從uri獲取位圖時獲取文件未發現異常

System.err的(3102):java.io.FileNotFoundException:沒有內容提供商:/storage/emulated/0/1.jpg

我試圖代碼,

Cursor mCursor; 
    mCursor=activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,mProjection,null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
     do { 
      String strPath = mCursor.getString(mCursor 
        .getColumnIndex(MediaStore.Images.Media.DATA)); 
         try { 
       Bitmap bitmap = MediaStore.Images.Media.getBitmap(
         activity.getContentResolver(), Uri.parse(strPath)); 
            //Here i'm using bitmap 
         } catch (FileNotFoundException e) { 
       Log.i("Exception", 
         "Image file not found: " + e.getMessage()); 
       e.printStackTrace(); 
         } catch (IOException e) { 
       Log.i("Exception", 
         "Input Output Failure: " + e.getMessage()); 
       e.printStackTrace(); 
      } 

     } while (mCursor.moveToNext()); 
+0

看一看這個問題>>> http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android -kitkat - 新的存儲訪問框架 – 2Dee

回答

0

GET路徑從URI和使用此路徑來獲得圖像

public String getRealPathFromURI(Uri contentUri) { 
    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     @SuppressWarnings("deprecation") 
     Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } catch (Exception e) { 
     return contentUri.getPath(); 
    } 
} 
0
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == 2 && resultCode == RESULT_OK) { 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      File file = new File(picturePath); 
      Bitmap bmpp = decodeAndResizeFile(file); 
// get bitmap that which image select you and set bmpp to imageview 

     }