Bitmap is returning null from BitmapFactory.decodeFile(filename)

Solution 1:

Hi it is null because may be the image size is big and getting exception please check your log and see is there any error of outofmemory bitmap if yes then use options for that:

BitmapFactory.Options options;

try {
  String imageInSD = "/sdcard/UserImages/" + userImageName;
  Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
  return bitmap;
} catch (OutOfMemoryError e) {
  try {
    options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeFile(imageInSD, null, options);
    return bitmap;
  } catch(Exception excepetion) {
    Log.e(excepetion);
  }
}

Solution 2:

Why are you doing this String imageInSD = "/sdcard/UserImages/"+userImageName;

I think If you get a .png file is present then just,

 Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

NOTE: Also check you have a Android supported image file is present in that location..

Solution 3:

It's simple: your file either is not an image or image that is not supported by Android's Bitmap implementation, or you path is invalid.


See documentation for BitmapFactory.decodeFile(String file):

Returns
the resulting decoded bitmap, or null if it could not be decoded.


Usually when bitmap cannot be decoded some logs are printed to logcat. Inspect them carefully.

Solution 4:

Be sure that in your options (BitmapFactory.Options) the InJustDecodeBounds is set to false or otherwise it will return null. This can be set to true when you just want the file to be decoded but you don't need it further in your code. This way no extra memory needs to be allocated. See here for more explanation.