BitmapFactory.decodeFile() returns null in some devices

三世轮回 提交于 2019-12-13 18:32:28

问题


I'm using following code from here. I want to compress image.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

int actualHeight = options.outHeight;
int actualWidth = options.outWidth;

After choose image from Camera, Gallery and Photos, i'm getting different type of paths in different devices based on OS type and device model. like:

1) /storage/emulated/0/Android/data/...

2) /raw//storage/emulated/0/mb/1511172993547.jpg

3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582

If path is like 1st url, this code is working fine. But if i get other types of images, then BitmapFactory.decodeFile() is giving null.

Is there any way to compress image in all types of devices and OS versions.

UPDATE :

To Open Picker :

Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 1001);

After choosing image :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri imgUri = Uri.fromFile(new File(data.getData().getPath()));
    Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
    dlgImageToPost.setImageBitmap(cmpBitmap);
...
}

For compression :

public static Bitmap compressUriQuality(Context mContext, Uri selectedImage) {
    InputStream imageStream = null;
    Bitmap bmp = null;
    try {
        imageStream = mContext.getContentResolver().openInputStream(
                selectedImage);
        bmp = BitmapFactory.decodeStream(imageStream);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 50, stream);

        if (imageStream != null)
            imageStream.close();
        stream.close();
        stream = null;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmp;
}

回答1:


Tv 2) /raw//storage/emulated/0/mb/1511172993547.jpg

That is not a path you would get if the user selected pictures from Gallery or Photos. And certainly it is no file system path you could use for .decodeFile(). How did you obtain it?

3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582

That is no valid content scheme path. How did you obtain it? And certainly cannot be used for .decodeFile().

  i'm getting different type of paths 

You will never get such paths if you 'act normal'. So what is it what you are doing? Elementary uri handling wrong?

using following code from here. 

That is pretty dirty example code for a big part as you have perceived now.

any way to compress image in all types of devices and OS versions.

Of course. Just use the obtained selected uri directly. Open an InputStream for it and use .decodeStream() instead.

My god.. you are not using the uri directly.

 Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);

Change to

  Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, data.getData());



回答2:


I had same issue, please add storage permission to your app.

Check this link for more info

Storage permission error in Marshmallow



来源:https://stackoverflow.com/questions/47390489/bitmapfactory-decodefile-returns-null-in-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!