[android][expo-image-picker] content:// URIs are not usable in `ExponentImagePicker.launchImageLibraryAsync`
Problem
Minimal reproducible example I don't have Android. I have no idea what is an `uri` with `content://` Summary "expo-image-picker": "~14.3.2", Exactly as it's happening here https://github.com/expo/expo/issues/9354, I'm getting an error in `ExponentImagePicker.launchImageLibraryAsync` [code block] For some reason, after upgrading to Expo 49, seems like the image picker totally changed (the UI) in Android, and it seems like it allows the user to pick photos that the URI are `content://xxxx` As you can see here, it's only happening since some days ago (when I published SDK 49 upgrade). <img width="323" alt="Screen Shot 2023-08-29 at 21 59 44" src="https://github.com/expo/expo/assets/1837969/9aef3e41-0f95-4725-b2a6-adea4533eb63"> Is there a workaround for this? Maybe some available flag to switch back to the old UI version, where files with `content://` uris were not visible. Current code: [code block] Environment [code block] "expo-image-picker": "~14.3.2",
Error Output
error in `ExponentImagePicker.launchImageLibraryAsync`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Workaround for Content URIs in Expo Image Picker
The issue arises from changes in the Expo Image Picker behavior starting from SDK 49, which now allows selection of images with 'content://' URIs that may not be compatible with the existing application logic. This change affects how URIs are handled and may lead to errors when trying to access or manipulate these URIs.
Awaiting Verification
Be the first to verify this fix
- 1
Check for Content URI Support
Before using the URI returned by the image picker, check if it starts with 'content://'. If it does, handle it appropriately to avoid errors.
typescriptif (uri.startsWith('content://')) { /* Handle content URI */ } - 2
Convert Content URI to File URI
If the URI is a content URI, convert it to a file URI using a helper function. This will ensure compatibility with your existing logic that may expect file URIs.
typescriptconst convertContentUriToFileUri = async (contentUri) => { /* Implement conversion logic */ }; - 3
Update Image Picker Call
Modify the call to `launchImageLibraryAsync` to include options that may help in filtering out unwanted URIs. Use the `mediaTypes` option to specify the types of media you want to allow.
typescriptconst result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images }); - 4
Test the Implementation
Run the application on an Android device or emulator to ensure that the image picker works as expected and that content URIs are handled correctly without causing errors.
typescriptconsole.log('Testing image picker...');
Validation
Confirm the fix by testing the image picker functionality on an Android device. Ensure that images can be selected without errors, and that content URIs are processed correctly. Check the console for any error messages during the selection process.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep