Search code examples
androidbitmap

Android App Resource load: IOException: Resource not found


I am getting an IOException when I am trying to load a bimpap image. Android Studio is able to parse the image. I do not have much of a background in app development so there might be something basic i am missing.

                Context context = getApplicationContext();
                ContentResolver contentResolver = context.getContentResolver();

                Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                        + "://" + context.getResources().getResourcePackageName(R.drawable.lenna_512x512)
                        + '/' + context.getResources().getResourceTypeName(R.drawable.lenna_512x512)
                        + '/' + context.getResources().getResourceEntryName(R.drawable.lenna_512x512));

                try (AssetFileDescriptor assetFileDescriptor
                             = contentResolver.openAssetFileDescriptor(imageUri, "r")) {
                    ParcelFileDescriptor parcelFileDescriptor = assetFileDescriptor.getParcelFileDescriptor();
                    int fd = parcelFileDescriptor.getFd();
                    tv.setText(imageProcLoadNeon(fd));
                    parcelFileDescriptor.close();
                } catch (IOException ioException) {
                    Toast.makeText(getApplicationContext(),"Image load failed",Toast.LENGTH_SHORT).show();
                    ioException.printStackTrace();
                    Log.e("MyCrappyApp", "image load error: " +  ioException.getMessage());
                }
            }

I get following error:

"E image load error:Resource does not exist: android.resource://com.learn.benchmarks/drawable/lenna_512x512"

Note that resource is present in resource is present in "res/drawable/lenna_512x512.bmp". I also tried to append ".bmp" at the end but still getting an IOException. I have tried clean and rebuild project as well, but still getting this issue.


Solution

  • You don't need to do this at all. If you want to access a drawable resource as a bitmap, you just do BitmapFactory.decodeResource(context.getResources(), R.drawable.image); You wouldn't use a URI. And a URI like that isn't a valid way to get a resource.