3

I attempt to read images from the internal storage, when I decode FileInputStream, BufferedInputStream or a File using BitmapFactory I get null as a result:

//mImages is an ArrayList of image file names, "a.jpg","b.jpg", etc.
//This is inside my custom adapter for returing ImageViews from mImages:

public View getView(int position, View ..., ViewGroup...){
Context base_context = MyApplication.getAppContext();

String currentImageFilename = mImages.get(position); //say this is "cat.jpg"

//after this line f = "/data/user/0/mobile.foo.bar/files/cat.jpg"
File f = base_context.getFileStreamPath(currentImageFilename);

Boolean ex = f.exists(); //returns true, inserted only for debugging as no 
//exception was thrown when decoding the bitmap and the result is null

BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(f));

Bitmap img = BitmapFactory.decodeStream(buffer); // img is null after this line
imageView.setImageBitmap(img);
}

I tried all other answers I could find, but no luck so far.

Dimitar Nikovski
  • 973
  • 13
  • 15
  • Try with smaller images. You took such big ones. – greenapps Mar 11 '17 at 20:59
  • Please tell the value of full path. You hide what you are doing with all those functions. Your f.exist() should be used in such a way that you display a toast if f does not exists and then return. Now f.exists() has no iinfluence on flow. – greenapps Mar 11 '17 at 21:02
  • Thanks @greenapps the .exists() is was only inserted, so that I can confirm to myself that the files are found as I debug, I didn't intend to keep it. The full path is now above in the edited version, the images are only 18kb each. – Dimitar Nikovski Mar 11 '17 at 21:46
  • Remove the BufferedInputStream. – greenapps Mar 11 '17 at 22:07
  • Still not working, not with decodeFile or decodeStream either. Yet, the file exists. What goes wrong is totally invisible, because when debugging as I go deeper step-by-step I get down to the native system API decoder and I can't see further... – Dimitar Nikovski Mar 12 '17 at 13:31

1 Answers1

5

If you're running this code on Android 7 and you use BitmapFactory.decodeStream you need to reset InputStream every time you want yo use it again. For example I used it twice, first to get some metrics and then to decode into Bitmap. And it worked fine on all versions prior to Android 7.

Now I need to reset it, otherwise it returns null:

BitmapFactory.decodeStream(iStream, null, options);
...
try {
    iStream.reset();
} catch (IOException e) {
    return null;
}
...
BitmapFactory.decodeStream(iStream, null, options);

Resetting inputStream won't cause any bugs on older versions so it's safe to use.

If it helped in your case - all credits to this guy: https://stackoverflow.com/a/41753686/5502121

Community
  • 1
  • 1
Kirill Karmazin
  • 6,256
  • 2
  • 54
  • 42
  • 1
    Thank you, it wasn't necessary in my case, I instead made a custom directory for the images upon saving and then used `this.getDir("MyImagesFolder", Context.MODE_PRIVATE).getAbsolutePath() + File.Separator + "myimage.jpg";` to read them and it worked. I appreaciate your help and it may help someone else who lands here. – Dimitar Nikovski Apr 23 '17 at 19:52
  • 1
    For Android >= 10 for me I had to re-call openInputStream each time before calling decodeStream when trying to use 2 different options – JunaidS Jun 30 '21 at 17:35