Android: decodeFile always returns null for file in internal storage
Asked Answered
I

8

21

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 the file as a resource and use ImageView.setImageResource, it always shows up fine.

What is the problem?

Here is the snippet:

filename = "test.png";

if (doesFileExist(filename))
    Bitmap bMap = BitmapFactory.decodeFile(filename);

I've also tried:

Bitmap bMap = BitmapFactory.decodeFile(getFilesDir().getPath()
                    + filename);
Infamy answered 16/5, 2011 at 3:57 Comment(1)
In decodeFile() you have to pass full pah of the file not just the name or not the way you are tryingSp
I
33

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

This was exactly what I needed:

String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
Infamy answered 16/5, 2011 at 6:6 Comment(1)
you SHOULD answer your own question. I landed on this question with the same problem and got the solution from you. The question you reference as having the answer wasn't in my initial search either. Cheers!Dibbuk
L
5

Instead of using BitmapFactory.decodeFile, try using InputStream:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    if(resultCode == RESULT_OK){          
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
Lime answered 21/9, 2016 at 12:58 Comment(0)
M
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);
Modigliani answered 16/5, 2011 at 5:17 Comment(1)
Good info to know, but in this case, the file is downloaded into the app's private storage and not in the resources.Gaberones
S
3

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.

Semela answered 22/10, 2018 at 10:22 Comment(0)
T
2

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

Thermionic answered 15/2, 2021 at 21:27 Comment(0)
I
0

Could you try fileList()? It

returns an array of strings naming the private files associated with this Context's application package.

Ill answered 16/5, 2011 at 4:47 Comment(1)
Actually, I used that to verify the file does in the Logcat output. So, the file is there.Gaberones
H
0

For me I was getting image from locally saved URL something like "file:///storage/emulated/0/...." (I have used Phonegap plugin to capture image. Plugin was giving me image path, which I need to use in native code)

Here is the code snippet which worked for me.

String captured_image_info = "file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg"
Uri uri=Uri.parse(captured_image_info);
largeLog("uri", "" + uri);

InputStream imageStream = getContentResolver().openInputStream(uri);

Bitmap bm = BitmapFactory.decodeStream(imageStream);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object

byte[] decodedBytes = baos.toByteArray();

Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
Homoiousian answered 3/5, 2017 at 11:16 Comment(0)
S
0

You need to setup run-time permissions for Manifest.permission.READ_EXTERNAL_STORAGE If you don't you'll get a null without getting any error message or even an indication in the Log. Note that you need to request the permissions both in the Manifest and at run time.

Sebastien answered 7/2, 2020 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.