[expo-media-library] getAssetsAsync causes the app to crash in Android (Expo Client and Standalone)
See original GitHub issue🐛 Bug Report
Summary of Issue
Calling MediaLibrary.getAssetsAsync
crashes the app on both Expo Client and standalone. There is no error logged and no indication of what could be causing the issue. This only happens in Android devices, works fine for iOS.
Environment - output of expo diagnostics
& the platform(s) you’re targeting
Expo CLI 3.28.2 environment info:
System:
OS: Linux 5.9 Manjaro Linux
Shell: 5.0.18 - /bin/bash
Binaries:
Node: 12.18.1 - ~/.config/nvm/versions/node/v12.18.1/bin/node
Yarn: 1.22.4 - ~/.config/nvm/versions/node/v12.18.1/bin/yarn
npm: 6.14.5 - ~/.config/nvm/versions/node/v12.18.1/bin/npm
npmGlobalPackages:
expo-cli: 3.28.2
Expo Workflow: managed
Expo SDK: 39
expo-media-library: 9.2.1
And the standalone app is running on a Pixel 4 with Android 11 (although this issue has been happening since Android 10).
Reproducible Demo
I created this snack with the code I’m currently using, however I cannot reproduce the same error in the snack for some reason.
Steps to Reproduce
const params = {
first: 59,
mediaType: [
MediaLibrary.MediaType.photo,
MediaLibrary.MediaType.video,
],
sortBy: [MediaLibrary.SortBy.modificationTime],
};
if (lastItemId) {
params.after = lastItemId;
}
try {
const library = await MediaLibrary.getAssetsAsync(params);
if (library.totalCount) {
if (lastItemId !== library.endCursor) {
setGallery([...gallery, ...library.assets]);
setLastItemId(library.endCursor);
}
}
} catch(e) {
Alert.alert(
'Gallery failed', 'An error happened fetching the gallery'
);
Logger.error('Error fetching gallery', e);
}
Nothing after const library = await MediaLibrary.getAssetsAsync(params)
gets executed and the app suddenly crashes.
Expected Behavior vs Actual Behavior
This is similar to some previous issues that are currently closed (#9440, #10168, #10488) and this is still happening in SDK 39. I have tested this same code in a physical iPhone and an iPhone simulator, with and without images to load, and has never crashed. I’m not sure if it is the same bug from the above mentioned tickets that is still persisting in SDK 39 or if this is a new one.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:22 (6 by maintainers)
Upgrade to SDK39 and MediaLibrary 9.2.1 (automatically if you upgrade via expo’s upgrade command) solves the problem.
Works on Android 11 as well. This thread saved my life. 🙏