From 1b28065a04083d945b7c67fecbd6686b9f5522fb Mon Sep 17 00:00:00 2001 From: phoenixli Date: Mon, 5 Jun 2023 17:39:41 -0700 Subject: [PATCH 1/4] Do not set requireOriginal on Android Picker uris. Android Picker (https://developer.android.com/training/data-storage/shared/photopicker) uris have MediaStore authority but does not accept requireOriginal. --- .../load/data/mediastore/MediaStoreUtil.java | 4 +++ .../model/stream/QMediaStoreUriLoader.java | 5 ++++ .../data/mediastore/MediaStoreUtilTest.java | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java diff --git a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java index bd00853e12..599152a120 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java +++ b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java @@ -20,6 +20,10 @@ public static boolean isMediaStoreUri(Uri uri) { && MediaStore.AUTHORITY.equals(uri.getAuthority()); } + public static boolean isAndroidPickerUri(Uri uri) { + return isMediaStoreUri(uri) && uri.getPathSegments().contains("picker"); + } + private static boolean isVideoUri(Uri uri) { return uri.getPathSegments().contains("video"); } diff --git a/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java b/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java index b19d655e3e..0ad102c8aa 100644 --- a/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java +++ b/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java @@ -156,6 +156,11 @@ private LoadData buildDelegateData() throws FileNotFoundException { if (Environment.isExternalStorageLegacy()) { return fileDelegate.buildLoadData(queryForFilePath(uri), width, height, options); } else { + // Android Picker uris have MediaStore authority and does not accept requireOriginal. + if (MediaStoreUtil.isAndroidPickerUri(uri)) { + return uriDelegate.buildLoadData(uri, width, height, options); + } + Uri toLoad = isAccessMediaLocationGranted() ? MediaStore.setRequireOriginal(uri) : uri; return uriDelegate.buildLoadData(toLoad, width, height, options); } diff --git a/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java b/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java new file mode 100644 index 0000000000..05d6ff565d --- /dev/null +++ b/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java @@ -0,0 +1,25 @@ +package com.bumptech.glide.load.data.mediastore; + +import static com.google.common.truth.Truth.assertThat; + +import android.net.Uri; +import android.provider.MediaStore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 18) +public class MediaStoreUtilTest { + + @Test + public void isAndroidPickerUri_identifiesAndroidPickerUris() { + Uri mediaStoreUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "123"); + Uri androidPickerUri = + Uri.parse("content://media/picker/0/com.android.providers.media.photopicker/media/123"); + + assertThat(MediaStoreUtil.isAndroidPickerUri(mediaStoreUri)).isFalse(); + assertThat(MediaStoreUtil.isAndroidPickerUri(androidPickerUri)).isTrue(); + } +} From 3f851993abab6683d1b3d36b528dcb5736603279 Mon Sep 17 00:00:00 2001 From: phoenixli Date: Mon, 5 Jun 2023 17:39:41 -0700 Subject: [PATCH 2/4] Do not set requireOriginal on Android Picker uris. Android Picker (https://developer.android.com/training/data-storage/shared/photopicker) uris have MediaStore authority but does not accept requireOriginal. --- .../load/data/mediastore/MediaStoreUtil.java | 6 ++++ .../model/stream/QMediaStoreUriLoader.java | 5 ++++ .../data/mediastore/MediaStoreUtilTest.java | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java diff --git a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java index bd00853e12..1a14cbe66c 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java +++ b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java @@ -20,6 +20,12 @@ public static boolean isMediaStoreUri(Uri uri) { && MediaStore.AUTHORITY.equals(uri.getAuthority()); } + // Android picker uris contains "picker" segment: + // https://android.googlesource.com/platform/packages/providers/MediaProvider/+/refs/heads/master/src/com/android/providers/media/PickerUriResolver.java#58 + public static boolean isAndroidPickerUri(Uri uri) { + return isMediaStoreUri(uri) && uri.getPathSegments().contains("picker"); + } + private static boolean isVideoUri(Uri uri) { return uri.getPathSegments().contains("video"); } diff --git a/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java b/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java index b19d655e3e..0ad102c8aa 100644 --- a/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java +++ b/library/src/main/java/com/bumptech/glide/load/model/stream/QMediaStoreUriLoader.java @@ -156,6 +156,11 @@ private LoadData buildDelegateData() throws FileNotFoundException { if (Environment.isExternalStorageLegacy()) { return fileDelegate.buildLoadData(queryForFilePath(uri), width, height, options); } else { + // Android Picker uris have MediaStore authority and does not accept requireOriginal. + if (MediaStoreUtil.isAndroidPickerUri(uri)) { + return uriDelegate.buildLoadData(uri, width, height, options); + } + Uri toLoad = isAccessMediaLocationGranted() ? MediaStore.setRequireOriginal(uri) : uri; return uriDelegate.buildLoadData(toLoad, width, height, options); } diff --git a/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java b/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java new file mode 100644 index 0000000000..6c84f79ac6 --- /dev/null +++ b/library/test/src/test/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtilTest.java @@ -0,0 +1,30 @@ +package com.bumptech.glide.load.data.mediastore; + +import static com.google.common.truth.Truth.assertThat; + +import android.net.Uri; +import android.provider.MediaStore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 18) +public class MediaStoreUtilTest { + + @Test + public void isAndroidPickerUri_notAndroidPickerUri_returnsFalse() { + Uri mediaStoreUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "123"); + + assertThat(MediaStoreUtil.isAndroidPickerUri(mediaStoreUri)).isFalse(); + } + + @Test + public void isAndroidPickerUri_identifiesAndroidPickerUri_returnsTrue() { + Uri androidPickerUri = + Uri.parse("content://media/picker/0/com.android.providers.media.photopicker/media/123"); + + assertThat(MediaStoreUtil.isAndroidPickerUri(androidPickerUri)).isTrue(); + } +} From a2d3da80f363316a0f954f811e437c15fd999f30 Mon Sep 17 00:00:00 2001 From: phoenixli Date: Thu, 8 Jun 2023 10:53:25 -0700 Subject: [PATCH 3/4] Fix a typo. --- .../com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java index 1a14cbe66c..c6de432016 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java +++ b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java @@ -20,7 +20,7 @@ public static boolean isMediaStoreUri(Uri uri) { && MediaStore.AUTHORITY.equals(uri.getAuthority()); } - // Android picker uris contains "picker" segment: + // Android picker uris contain "picker" segment: // https://android.googlesource.com/platform/packages/providers/MediaProvider/+/refs/heads/master/src/com/android/providers/media/PickerUriResolver.java#58 public static boolean isAndroidPickerUri(Uri uri) { return isMediaStoreUri(uri) && uri.getPathSegments().contains("picker"); From 08363e14f1259c4a8563e7956c733c63bef1c5f7 Mon Sep 17 00:00:00 2001 From: phoenixli Date: Thu, 8 Jun 2023 10:54:30 -0700 Subject: [PATCH 4/4] Fix a typo. --- .../com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java index c6de432016..46951a91e7 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java +++ b/library/src/main/java/com/bumptech/glide/load/data/mediastore/MediaStoreUtil.java @@ -20,7 +20,7 @@ public static boolean isMediaStoreUri(Uri uri) { && MediaStore.AUTHORITY.equals(uri.getAuthority()); } - // Android picker uris contain "picker" segment: + // Android picker uris contain a "picker" segment: // https://android.googlesource.com/platform/packages/providers/MediaProvider/+/refs/heads/master/src/com/android/providers/media/PickerUriResolver.java#58 public static boolean isAndroidPickerUri(Uri uri) { return isMediaStoreUri(uri) && uri.getPathSegments().contains("picker");