Simon Dzn
Simon Dzn

Reputation: 150

BitmapFactory.decodeFile is null

I tried several ways but it always returns 'null'.

I want to read a file from the camera folder into a bitmap object.

File camDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera");
    File[] files = camDir.listFiles();
    Bitmap img = BitmapFactory.decodeFile(files[1].getAbsolutePath());
    img.getDensity();

Where is my mistake?

Upvotes: 1

Views: 639

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

If you don't know what's the null you can add a simple try()catch(Exception e) and then e.printstacktrace(); to get the error.

If the error is about outOfMemory try this answer;

Also, you may try this trick :

File camDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera");
File[] files = camDir.listFiles();
FileInputStream FI = new FileInputStream (files[1].getAbsolutePath());
Bitmap img = BitmapFactory.decodeStream(FI);
img.getDensity();

Another try that I mentioned to you on comments you can remove the getAbsolutePath() and do it as follows :

File camDir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");

Upvotes: 1

Simon Dzn
Simon Dzn

Reputation: 150

Thank you all for the help!!

I found that the problem was the "getAbsolutePath()" I don't understand why but it works..

I changed this:

File camDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera");

To:

File camDir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");

And it works!

Upvotes: 1

Related Questions