FileNotFound exception while loading bitmap images from Android gallery

余生长醉 提交于 2019-12-11 12:52:35

问题


I am trying to create a bitmap image from my Android gallery. I do this in an AsyncTask and this is the reason why I have a cursor in my implementation. The code is the following

                try {
                uri = Uri.parse(uriStr);
            } catch (Exception e) {
                Log.e(LOG_TAG, "problem with the image loading: " + e);
            }
                if(uri == null)
                    continue;
                Bitmap bm = null;
                try{
                    bm = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), uri);
                } catch (FileNotFoundException e) {
                    Log.e(LOG_TAG, "Bitmap not found: " + e);
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Bitmap IO error: " + e);

When I debug, I notice there is a FileNotFoundException thrown, the output:

08-17 13:47:55.754 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ java.io.FileNotFoundException: No content provider: /storage/emulated/0/Download/unnamed.jpg 08-17 13:47:55.833 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1086) 08-17 13:47:55.921 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:941) 08-17 13:47:55.975 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openInputStream(ContentResolver.java:666) 08-17 13:47:56.019 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:849) 08-17 13:47:56.062 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at com.example.jonas.shoppinglist.processes.ShoppingGallery.doInBackground(ShoppingGallery.java:51)

The result is that I didn't get one single bitmap image of my gallery. What is the problem?


回答1:


you can try below code for achiving proper image path from gallery .....


public void pickImage() {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 1000);
        }

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 1000 && resultCode == Activity.RESULT_OK) {
                if (data == null) {
                    // Display an error
                    return;
                }

                if (Build.VERSION.SDK_INT < 11)
                    ImagePath = RealPathUtil.getRealPathFromURI_BelowAPI11(this,
                            data.getData());

                // SDK >= 11 && SDK < 19
                else if (Build.VERSION.SDK_INT < 19)
                    ImagePath = RealPathUtil.getRealPathFromURI_API11to18(this,
                            data.getData());

                // SDK > 19 (Android 4.4)
                else
                    ImagePath = RealPathUtil.getRealPathFromURI_API19(this,
                            data.getData());

            }
        }
    -----------------------------------
    //class 
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.CursorLoader;
    import android.database.Cursor;
    import android.net.Uri;
    import android.provider.DocumentsContract;
    import android.provider.MediaStore;

    public class RealPathUtil {

        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API19(Context context, Uri uri){
            String filePath = "";
            String wholeID = DocumentsContract.getDocumentId(uri);

             // Split at colon, use second item in the array
             String id = wholeID.split(":")[1];

             String[] column = { MediaStore.Images.Media.DATA };     

             // where id is equal to             
             String sel = MediaStore.Images.Media._ID + "=?";

             Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                       column, sel, new String[]{ id }, null);

             int columnIndex = cursor.getColumnIndex(column[0]);

             if (cursor.moveToFirst()) {
                 filePath = cursor.getString(columnIndex);
             }   
             cursor.close();
             return filePath;
        }


        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
              String[] proj = { MediaStore.Images.Media.DATA };
              String result = null;

              CursorLoader cursorLoader = new CursorLoader(
                      context, 
                contentUri, proj, null, null, null);        
              Cursor cursor = cursorLoader.loadInBackground();

              if(cursor != null){
               int column_index = 
                 cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
               cursor.moveToFirst();
               result = cursor.getString(column_index);
              }
              return result;  
        }

        public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
                   String[] proj = { MediaStore.Images.Media.DATA };
                   Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
                   int column_index
              = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                   cursor.moveToFirst();
                   return cursor.getString(column_index);
        }
    }


来源:https://stackoverflow.com/questions/32049962/filenotfound-exception-while-loading-bitmap-images-from-android-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!