Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

android - Bitmap is returning null from BitmapFactory.decodeFile(filename)

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;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...