Bitmap is returning null from BitmapFactory.decodeFile(filename)

前端 未结 14 1118
悲哀的现实
悲哀的现实 2020-11-29 04:19

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 cod

14条回答
  •  春和景丽
    2020-11-29 04:59

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

提交回复
热议问题