Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap is returning null from BitmapFactory.decodeFile(filename)

Tags:

android

When I am calling this function there is no image in image view bitmapFactory.decodefile(filename) showing null .. please help for this.

Here is my code :

public Bitmap ShowImage(String imageName,String userImageName )  {      File sdcard_mainDirectory = new File(Environment.getExternalStorageDirectory(),"UserImages").getAbsoluteFile();      File file = new File(sdcard_mainDirectory, userImageName).getAbsoluteFile();      if (file != null) {          try {              String imageInSD = "/sdcard/UserImages/"+userImageName;              Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);              return bitmap;          }         catch (Exception e) {              e.printStackTrace();         }      }      return null;  } 
like image 350
Sumit Patel Avatar asked Dec 09 '11 07:12

Sumit Patel


2 Answers

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);   } } 
like image 146
kalpana c Avatar answered Oct 14 '22 17:10

kalpana c


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

like image 40
user370305 Avatar answered Oct 14 '22 19:10

user370305