1

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?

Simon Dzn
  • 150
  • 1
  • 1
  • 8

2 Answers2

1

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");
Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
1

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!

Simon Dzn
  • 150
  • 1
  • 1
  • 8