Bitmap is returning null from BitmapFactory.decodeFile(filename)

前端 未结 14 1057
悲哀的现实
悲哀的现实 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:55

    The BitmapFactory.decodeFile() executes before the entire image placing in that exact path. When decodeFile() executes there is no image. So the bitmap returns null. Generally the high pixel images takes some extra time to place in their path. So this exception happens.

    Please check the null and try to decodeFile.

    Bitmap bitmap = null;
    
    while(bitmap == null)
       bitmap = BitmapFactory.decodeFile(imageInSD);
    
    0 讨论(0)
  • 2020-11-29 04:56

    Delete "options.inJustDecodeBounds = true;" if you have

      val options =  BitmapFactory.Options();
      //options.inJustDecodeBounds = true; //delete this line
      var bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    
    0 讨论(0)
  • 2020-11-29 04:58

    Try this code it will help you to solve your problem

    Replace gallery image with gallery image in android show too large exception

    0 讨论(0)
  • 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);
      }
    }
    
    0 讨论(0)
  • 2020-11-29 05:00

    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.

    0 讨论(0)
  • 2020-11-29 05:02

    My solution was to add to the manifest's Applicaiton tag:

    Android: requestLegacyExternalStorage = "true"
    
    0 讨论(0)
提交回复
热议问题