I am using BitmapFactory.decodeFile to load Bitmaps of images into my application. However, the function returns null on large images (such as those from the camera). The filepath is definitely correct, I just can’t figure out why it would return null. I tried supersampling, but it didn’t seem to help.
Does anyone have any idea why it would do this or how I could more easily load images taken from the camera into a Bitmap?
Here’s the code I am using:
public static Bitmap loadBitmap(String filePath){
Bitmap result = BitmapFactory.decodeFile(filePath);
if(result == null){
if(filePath.contains(".jpg") || filePath.contains(".png")){
//This is the error that occurs when I attempt to load an image from the Camera DCIM folder or a large png I imported from my computer.
Utils.Toast("Could not load file -- too big?");
} else {
Utils.Toast("Could not load file -- image file type is not supported");
}
}
return result;
}
You will need to provide more info about your problem, such as a snippet of code that you are using. If you want to know when/why the
BitmapFactory.decodeFilemethod would return null, you can read directly its source code: http://casidiablo.in/BitmapFactoryFor example, one of the reasons that causes
BitmapFactory.decodeFileto return null is if there’s a problem while openning the file. Curiously, the developers dont’t log anything with such a problem… look at the comment “do nothing. If the exception happened on open, bm will be null.”As you can see, the
BitmapFactory.decodeFiledoes not work standalone… but it uses some other methods of theBitmapFactoryclass (for instance,BitmapFactory.decodeStream,BitmapFactory.nativeDecodeStream,BitmapFactory.finishDecode, etc.). The problem could be on one of those methods, so if I were you, I would try to read and understand how they work so that I could know in which cases they return null.