From 87a32fb6c1ffbae8d79e6f313666a9a554987aa5 Mon Sep 17 00:00:00 2001 From: Gabor Gyorvari Date: Thu, 10 Sep 2020 14:36:26 +0200 Subject: [PATCH 1/2] Android 10 scoped storage fix --- .../src/MultiImageChooserActivity.java | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/android/Library/src/MultiImageChooserActivity.java b/src/android/Library/src/MultiImageChooserActivity.java index ba379e3b..4a2910aa 100644 --- a/src/android/Library/src/MultiImageChooserActivity.java +++ b/src/android/Library/src/MultiImageChooserActivity.java @@ -47,6 +47,7 @@ import android.app.AlertDialog; import android.app.LoaderManager; import android.app.ProgressDialog; +import android.content.ContentUris; import android.content.Context; import android.content.CursorLoader; import android.content.DialogInterface; @@ -57,9 +58,11 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; +import android.graphics.ImageDecoder; import android.graphics.Matrix; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.ActionBar; @@ -522,7 +525,11 @@ protected ArrayList doInBackground(Set>... fileSe BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(file.getAbsolutePath(), options); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { + _tryToGetBitmap(file, options); + } else { + BitmapFactory.decodeFile(file.getAbsolutePath(), options); + } int width = options.outWidth; int height = options.outHeight; float scale = calculateScale(width, height); @@ -620,15 +627,45 @@ protected void onPostExecute(ArrayList al) { finish(); } + private Bitmap _tryToGetBitmap(File file, BitmapFactory.Options options) throws IOException, OutOfMemoryError { + Bitmap bmp = null; + String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DISPLAY_NAME}; + String selection = MediaStore.Images.ImageColumns.DISPLAY_NAME + " = ?"; + String[] selectionArguments = {file.getName()}; + Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArguments, null); + if (cursor != null && cursor.getCount() > 0) { + cursor.moveToFirst(); + Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID))); + cursor.close(); + bmp = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), imageUri)); + } + + if (bmp == null) { + throw new IOException("The image file could not be opened."); + } + + if (options != null && options.inJustDecodeBounds) { + options.outWidth = bmp.getWidth(); + options.outHeight = bmp.getHeight(); + return null; + } + + return bmp; + } + private Bitmap tryToGetBitmap(File file, BitmapFactory.Options options, int rotate, boolean shouldScale) throws IOException, OutOfMemoryError { Bitmap bmp; - if (options == null) { - bmp = BitmapFactory.decodeFile(file.getAbsolutePath()); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { + bmp = _tryToGetBitmap(file, options); } else { - bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + if (options == null) { + bmp = BitmapFactory.decodeFile(file.getAbsolutePath()); + } else { + bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + } } if (bmp == null) { From 838d56328ef6eb95e57faf9b2df97167b886a3f0 Mon Sep 17 00:00:00 2001 From: Gabor Gyorvari Date: Wed, 16 Sep 2020 11:47:52 +0200 Subject: [PATCH 2/2] Bounds retrived from MediaStore and added sample size functinality --- .../src/MultiImageChooserActivity.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/android/Library/src/MultiImageChooserActivity.java b/src/android/Library/src/MultiImageChooserActivity.java index 4a2910aa..ea3649a5 100644 --- a/src/android/Library/src/MultiImageChooserActivity.java +++ b/src/android/Library/src/MultiImageChooserActivity.java @@ -65,6 +65,7 @@ import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; +import android.support.annotation.RequiresApi; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.util.Base64; @@ -627,29 +628,42 @@ protected void onPostExecute(ArrayList al) { finish(); } + @RequiresApi(api = Build.VERSION_CODES.P) private Bitmap _tryToGetBitmap(File file, BitmapFactory.Options options) throws IOException, OutOfMemoryError { Bitmap bmp = null; - String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DISPLAY_NAME}; + String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.WIDTH, MediaStore.Images.ImageColumns.HEIGHT}; String selection = MediaStore.Images.ImageColumns.DISPLAY_NAME + " = ?"; String[] selectionArguments = {file.getName()}; Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArguments, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); + + if (options != null && options.inJustDecodeBounds) { + options.outWidth = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.WIDTH)); + options.outHeight = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.HEIGHT)); + cursor.close(); + + return null; + } + Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID))); cursor.close(); - bmp = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), imageUri)); + + if (options != null) { + bmp = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), imageUri), new ImageDecoder.OnHeaderDecodedListener() { + public void onHeaderDecoded(ImageDecoder decoder, ImageDecoder.ImageInfo info, ImageDecoder.Source source) { + decoder.setTargetSampleSize(options.inSampleSize); + } + }); + } else { + bmp = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), imageUri)); + } } if (bmp == null) { throw new IOException("The image file could not be opened."); } - if (options != null && options.inJustDecodeBounds) { - options.outWidth = bmp.getWidth(); - options.outHeight = bmp.getHeight(); - return null; - } - return bmp; }