Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 10 scoped storage fix #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/android/Library/src/MultiImageChooserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,11 +58,14 @@
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.annotation.RequiresApi;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
Expand Down Expand Up @@ -522,7 +526,11 @@ protected ArrayList<String> doInBackground(Set<Entry<String, Integer>>... 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);
Expand Down Expand Up @@ -620,15 +628,58 @@ protected void onPostExecute(ArrayList<String> 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, 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();

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.");
}

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) {
Expand Down