2

I tried to get the image bitmap for a URI but it is giving me the following error.

System.err(3102): java.io.FileNotFoundException: No content provider: /storage/emulated/0/1.jpg

The code i tried is,

    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());
Bhupesh
  • 477
  • 1
  • 8
  • 22
  • Have a look at this question >>> http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework – 2Dee Jan 22 '14 at 09:08

2 Answers2

1
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 

        }
Dakshesh Khatri
  • 639
  • 7
  • 12
0

get path from uri and use this path to get image

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();
    }
}
Vijju
  • 3,458
  • 1
  • 22
  • 20