BitmapFactory.decodeFile always returns null on Xiaomi

Hello I have this piece of code:

EDIT:

imageName = data.getData();

try{
    InputStream stream = getContentResolver().openInputStream(imageName);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    bitmap = BitmapFactory.decodeStream(stream,null,options);

    final int REQUIRED_WIDTH=(int)screenWidth;
    final int REQUIRED_HIGHT=(int)screenHeight;

    int scale=1;
    while(options.outWidth/scale/2>=REQUIRED_WIDTH && options.outHeight/scale/2>=REQUIRED_HIGHT)
        scale*=2;

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize=scale;
    bitmap = BitmapFactory.decodeStream(stream, null, o2);

    if ( bitmap != null ){
        ok=true;
    }
}catch(Exception e){
    Toast.makeText(getBaseContext(),"error", Toast.LENGTH_SHORT).show();
}

      

But the bitmap is still null

Can anyone tell me why? or better ... How to fix it?

0


source to share


2 answers


Ok, so if I do it like now in the edited post, how can I resize the image to get rid of the outOfMemory Exception?

Aha! Now we get somewhere.

The previous rule for your question was:

  • read the entire bitmap into a byte array
  • write the entire bitmap to another byte array as a low quality JPEG
  • read the entire bitmap into Bitmap

    , supported by another byte array

This will result in your consuming ~ 2.1x a bunch of space of your current implementation which is already giving you messages OutOfMemoryError

. Byte arrays from # 1 and # 3 above will be the same size, equal to:

width x height x 4

      



where the width and height are in pixels.

To reduce memory consumption, you need to do the following:

  • Read through the bitmap file once , as the current code does.

  • Use BitmapFactory.Options

    to control bitmap decoding. In particular, useinSampleSize

    to reduce the number of pixels in the resulting Bitmap

    . Quoting the JavaDocs for inSampleSize

    :

If set to> 1, asks the decoder to sub-sample the original image, returning a smaller image to save memory. The sample size is the number of pixels in any dimension that corresponds to one pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image equal to 1/4 the width / height of the original and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: The decoder uses an end value based on powers of 2, any other value will be rounded to the nearest power of 2.

This sample project demonstrates usage inSampleSize

for various hardcoded values. The actual download Bitmap

comes from:

private Bitmap load(String path, int inSampleSize) throws IOException {
  BitmapFactory.Options opts=new BitmapFactory.Options();

  opts.inSampleSize=inSampleSize;

  return(BitmapFactory.decodeStream(assets().open(path), null, opts));
}

      

0


source


Your getPathFromUri method is not working. It should return something like /storage/emulated/0/DCIM/camera/bla bla.jpg

. file://

is an authority and is not part of the path. BitmapFactory looks for the actual file path.

You can remove this manually or use the Uri class with something like this:



Uri imgUri = Uri.parse(imgPath);
String filePath = imgUri.getPath();

      

0


source