From dc1139a8aff55a014af04698d3d486fb0518cfdb Mon Sep 17 00:00:00 2001 From: fgui Date: Mon, 30 Dec 2019 18:45:11 +0800 Subject: [PATCH] adapt Android10/Q --- sample/build.gradle | 6 +- ucrop/build.gradle | 5 +- .../yalantis/ucrop/task/BitmapLoadTask.java | 102 +++++++++++------- 3 files changed, 70 insertions(+), 43 deletions(-) diff --git a/sample/build.gradle b/sample/build.gradle index 233cd2063..c721e31b6 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 28 - buildToolsVersion '28.0.3' + compileSdkVersion 29 + defaultConfig { applicationId "com.yalantis.ucrop.sample" minSdkVersion 14 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 13 versionName "1.2.4" } diff --git a/ucrop/build.gradle b/ucrop/build.gradle index f6490e585..813ae857c 100644 --- a/ucrop/build.gradle +++ b/ucrop/build.gradle @@ -2,12 +2,11 @@ apply plugin: 'com.android.library' apply from: '../mavenpush.gradle' android { - compileSdkVersion 28 - buildToolsVersion '28.0.3' + compileSdkVersion 29 defaultConfig { minSdkVersion 14 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 25 versionName "2.2.4-native" diff --git a/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java b/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java index c836fdd11..68efbb395 100755 --- a/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java @@ -1,10 +1,13 @@ package com.yalantis.ucrop.task; import android.Manifest.permission; +import android.annotation.SuppressLint; +import android.content.ContentResolver; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.ImageDecoder; import android.graphics.Matrix; import android.net.Uri; import android.os.AsyncTask; @@ -12,6 +15,11 @@ import android.os.ParcelFileDescriptor; import android.text.TextUtils; import android.util.Log; +import android.util.Size; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import com.yalantis.ucrop.callback.BitmapLoadCallback; import com.yalantis.ucrop.model.ExifInfo; @@ -26,9 +34,6 @@ import java.io.InputStream; import java.io.OutputStream; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -45,6 +50,7 @@ public class BitmapLoadTask extends AsyncTask= Build.VERSION_CODES.JELLY_BEAN) { + BitmapLoadUtils.close(parcelFileDescriptor); + } + } else { try { - decodeSampledBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); - decodeAttemptSuccess = true; - } catch (OutOfMemoryError error) { - Log.e(TAG, "doInBackground: BitmapFactory.decodeFileDescriptor: ", error); - options.inSampleSize *= 2; + ImageDecoder.Source source = ImageDecoder.createSource(resolver, mInputUri); + decodeSampledBitmap = ImageDecoder.decodeBitmap(source, new ImageDecoder.OnHeaderDecodedListener() { + @SuppressLint("NewApi") + @Override + public void onHeaderDecoded(@NonNull ImageDecoder decoder, @NonNull ImageDecoder.ImageInfo info, @NonNull ImageDecoder.Source source) { + Size size = info.getSize(); + if (size.getWidth() == -1 || size.getWidth() == -1) { + throw new IllegalArgumentException("Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]"); + } + } + }); + } catch (IllegalArgumentException e) { + return new BitmapWorkerResult(e); + } catch (IOException e) { + Log.e(TAG, "doInBackground: ImageDecoder.decodeBitmap: ", e); } } @@ -136,10 +168,6 @@ protected BitmapWorkerResult doInBackground(Void... params) { return new BitmapWorkerResult(new IllegalArgumentException("Bitmap could not be decoded from the Uri: [" + mInputUri + "]")); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - BitmapLoadUtils.close(parcelFileDescriptor); - } - int exifOrientation = BitmapLoadUtils.getExifOrientation(mContext, mInputUri); int exifDegrees = BitmapLoadUtils.exifToDegrees(exifOrientation); int exifTranslation = BitmapLoadUtils.exifToTranslation(exifOrientation); @@ -172,7 +200,7 @@ private void processInputUri() throws NullPointerException, IOException { } } else if ("content".equals(inputUriScheme)) { String path = getFilePath(); - if (!TextUtils.isEmpty(path) && new File(path).exists()) { + if (!TextUtils.isEmpty(path) && new File(path).exists() && mBeforeAndroidQ) { mInputUri = Uri.fromFile(new File(path)); } else { try { @@ -213,7 +241,7 @@ private void copyFile(@NonNull Uri inputUri, @Nullable Uri outputUri) throws Nul throw new NullPointerException("InputStream for given input Uri is null"); } - byte buffer[] = new byte[1024]; + byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length);