BitmapFactory.decodeFile() returns null in some devices
Asked Answered
M

2

2

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;
}
Marieann answered 20/11, 2017 at 11:2 Comment(4)
you have image Uri, so open its InputStream and decode it using BitmapFactory - you can get InputStream by using ContentResolver APIAntre
are you trying to get image path from galley or any other path ???Vern
and no, do not use any "image path" - all you need is UriAntre
Thank you for commenting, i'll try using UriMarieann
B
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());
Bienne answered 20/11, 2017 at 11:54 Comment(7)
Thank you for reply, i'll try to use UrlMarieann
Error coming on using Uri also, when i choose image from photos in Xiomi, data.getData().getPath() returns url like '/-1/1/content://media/external/images/media/7741/ORIGINAL/NONE/1544403096'. In this case, mContext.getContentResolver().openInputStream(selectedImageUri) is giving FileNotFoundException.Marieann
Please show complete code. Put the code in your post. Not in a comment. Unreadable. Also post complete exception message from logcat. IN YOUR POST.Bienne
The content scheme is in data.getData().toString(). Dont use getPath().Bienne
Also show with which intent you obtained the uri. You did not answer: How did you obtain it?.Bienne
I updated code, i also tried with data.getData().toString() but that also returns same exception.Marieann
You should not try uri.toString() you should use the uri directly. See my answer again.Bienne
S
1

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

Check this link for more info

Storage permission error in Marshmallow

Soto answered 20/11, 2017 at 11:15 Comment(0)
B
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());
Bienne answered 20/11, 2017 at 11:54 Comment(7)
Thank you for reply, i'll try to use UrlMarieann
Error coming on using Uri also, when i choose image from photos in Xiomi, data.getData().getPath() returns url like '/-1/1/content://media/external/images/media/7741/ORIGINAL/NONE/1544403096'. In this case, mContext.getContentResolver().openInputStream(selectedImageUri) is giving FileNotFoundException.Marieann
Please show complete code. Put the code in your post. Not in a comment. Unreadable. Also post complete exception message from logcat. IN YOUR POST.Bienne
The content scheme is in data.getData().toString(). Dont use getPath().Bienne
Also show with which intent you obtained the uri. You did not answer: How did you obtain it?.Bienne
I updated code, i also tried with data.getData().toString() but that also returns same exception.Marieann
You should not try uri.toString() you should use the uri directly. See my answer again.Bienne

© 2022 - 2024 — McMap. All rights reserved.