Bitmap is returning null from BitmapFactory.decodeFile(filename)

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);
  }
}

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..


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.

Tags:

Android