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

refactor: Use AndroidX Photo Picker for selecting media #2304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hubatruck
Copy link

@hubatruck hubatruck commented Jun 7, 2024

Thanks for submitting a PR! Please read these instructions carefully:

  • Explain the motivation for making this change.
  • Provide a test plan demonstrating that the code is solid.
  • Match the code formatting of the rest of the codebase.
  • Target the main branch, NOT a "stable" branch.

Motivation (required)

What existing problem does the pull request solve?

For a more privacy preserving experience for users, we’re introducing the Photo and Video Permissions policy to reduce the number of apps permitted to request broad photo/video permissions (READ_MEDIA_IMAGES and READ_MEDIA_VIDEO). Apps may only access photos and videos for purposes directly related to app functionality. Apps that have a one-time or infrequent need to access these files are requested to use a system picker, such as the Android photo picker. (effective August 31, 2024)

Reference: https://support.google.com/googleplay/android-developer/answer/13986130

Converted the launchImageLibrary Android function to use the native AndroidX Photo Picker: https://developer.android.com/training/data-storage/shared/photopicker

Test Plan (required)

Tested out on multiple devices, both with the example app, and on my own project.

  • Tried selecting single image
  • Tried selecting multiple images
  • Tried selecting single video
  • Tried selecting multiple videos
  • Tried selecting single video/image
  • Tried selecting multiple videos/images

Devices used:

Make Android version
Samsung SM-A520F Android 8
Samsung SM-A750F Android 10
Pixel 3 Android 11
Samsung Galaxy Tab A8 Android 13

Relevant issue: #2146

@aranda-adapptor
Copy link

Thanks for submitting @hubatruck. We can confirm that the update works well for our use case, and means all permission checks can be removed once targeting SDK 34. We haven't tested this update when targeting versions prior 34 though.

@Darex1991
Copy link

@hubatruck Am I doing something wrong? I have errors:

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:28: error: cannot find symbol
import androidx.activity.result.PickVisualMediaRequest;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:30: error: cannot find symbol
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:31: error: cannot find symbol
import androidx.activity.result.contract.ActivityResultContracts.PickMultipleVisualMedia;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:126: error: package PickVisualMedia does not exist
				PickVisualMedia.VisualMediaType mediaType;

I have target SDK 34 and I added to app/build.gradle

implementation("androidx.activity:activity:1.9.0")
implementation("androidx.activity:activity-compose:1.9.0")
implementation("androidx.activity:activity-ktx:1.9.0")

@hubatruck
Copy link
Author

@hubatruck Am I doing something wrong? I have errors:

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:28: error: cannot find symbol
import androidx.activity.result.PickVisualMediaRequest;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:30: error: cannot find symbol
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:31: error: cannot find symbol
import androidx.activity.result.contract.ActivityResultContracts.PickMultipleVisualMedia;

react-native-image-picker/android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java:126: error: package PickVisualMedia does not exist
				PickVisualMedia.VisualMediaType mediaType;

I have target SDK 34 and I added to app/build.gradle

implementation("androidx.activity:activity:1.9.0")
implementation("androidx.activity:activity-compose:1.9.0")
implementation("androidx.activity:activity-ktx:1.9.0")

Hi @Darex1991

Could you please give a bit more information about your issue?

I have ran the project with the example application which also uses Kotlin, and it was not throwing me this kind of error. So it seems like react-native has androidx.activity dependency already included, and no need for additional action. Running ./gradle app:dependencies inside the example/android folder also confirmed this.

For easier debugging, the best would be a minimal project that reproduces the issue you mentioned.

@janithl
Copy link

janithl commented Jul 25, 2024

@Darex1991 try adding these to react-native-image-picker's android/build.gradle:

implementation "androidx.core:core:1.3.1"
implementation "androidx.activity:activity:1.6.0"
implementation "androidx.fragment:fragment:1.5.7"

@janithl
Copy link

janithl commented Jul 25, 2024

@hubatruck First of all, thank you for this fix! It's been a lifesaver.

I'm having an issue on Android 11/12 where the ACTION_OPEN_DOCUMENT is fired and the user is asked to give the app permission to access the media files once an image is selected. Do you know if this violates Google's new photo and video policy?

@Darex1991
Copy link

@janithl this helped me! Thanks.
Do you know if I still need to check READ_EXTERNAL_STORAGE permission?
For now, on Androids < 13, I check [PERMISSIONS.IOS.PHOTO_LIBRARY, PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE]

@janithl
Copy link

janithl commented Jul 25, 2024

@Darex1991 I'm glad I could help. I'm not 100% sure about the usage of READ_EXTERNAL_STORAGE for older versions of Android either, but I've been looking at expo packages and they seem to use it with a android:maxSdkVersion="32" in their manifests.

The Photo Picker also falls back to opening documents on older versions of Android if the modular component is not available, which seems to require some sort of read permission:

If the photo picker isn't available on a device, the library automatically invokes the ACTION_OPEN_DOCUMENT intent action instead. This intent is supported on devices that run Android 4.4 (API level 19) or higher. You can verify whether the photo picker is available on a given device by calling isPhotoPickerAvailable().

@janithl
Copy link

janithl commented Jul 25, 2024

@Darex1991 I hope this post from the Android Developers blog is helpful: https://medium.com/androiddevelopers/permissionless-is-the-future-of-storage-on-android-3fbceeb3d70a

@Darex1991
Copy link

@janithl I only noticed adding implementation "androidx.activity:activity:1.6.0" increasing my build twice 36MB -> 72MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants