Skip to content Skip to sidebar Skip to footer

Android: Decodefile Always Returns Null For File In Internal Storage

I have a file saved locally into the application's private storage. I have verified it exists, however whenever I call BitmapFactory.decodeFile it always returns null. If I save th

Solution 1:

This question has been answered before such as here: BitmapFactory.decodeFile returns null even image exists

This was exactly what I needed:

String fname=newFile(getFilesDir(), "test.png").getAbsolutePath();

Solution 2:

Instead of using BitmapFactory.decodeFile, try using InputStream:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    if(resultCode == RESULT_OK){          
        UriselectedImage= imageReturnedIntent.getData();
        InputStreamimageStream= getContentResolver().openInputStream(selectedImage);
        BitmapyourSelectedImage= BitmapFactory.decodeStream(imageStream);

Solution 3:

Folks, files stored in app resource should be referenced in special way. E.g. if file is located in assets and named as "myfile.png" it has to be referenced as:

String uriString="file:///android_asset/myfile.png";
Uri uri=Uri.parse(uriString);

Solution 4:

BitmapFactory.decodeFile expects a file path without the scheme. I.e. without the file:// in the beginning.

If you're handling a Uri, don't just .toString() it, but instead call .getPath() on it, and pass that to the method.

Solution 5:

After adding the required permissions (READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE)

You need to add this line to manifests: android:requestLegacyExternalStorage="true"

enter image description here

Post a Comment for "Android: Decodefile Always Returns Null For File In Internal Storage"