Expo-media-library createAssetAsync calls error in Android 11 even with MediaLibrary Permission.
See original GitHub issueSummary
Expo-media-library createAssetAsync calls error “Unable to copy file into external storage.” in Android 11 even with MediaLibrary Permission. MediaLibrary does not require MANAGE_EXTERNAL_STORAGE permission. But always show error message in Android 11. Works fine in others. Both SDK 40 and SDK 41 give the same result.
Is there a way to replace it with StorageAccessFramework? Isn’t SAF only able to create string or empty files? I tried to grant access to the DCIM folder using SAF, but createAssetAsync still doesn’t work.
Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!
managed
What platform(s) does this occur on?
Android
SDK Version (managed workflow only)
40.0.0, 41.0.0.
Environment
Expo CLI 4.1.6 environment info: System: OS: Windows 10 10.0.19041 Binaries: Node: 14.15.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.10 - C:\Users\Administrator\AppData\Roaming\npm\yarn.CMD npm: 6.14.9 - C:\Users\Administrator\AppData\Roaming\npm\npm.CMD IDEs: Android Studio: Version 4.1.0.0 AI-201.8743.12.41.6953283 npmPackages: expo: ^40.0.0 => 40.0.1 (And 41.0.0) react: 16.13.1 => 16.13.1 react-dom: 16.13.1 => 16.13.1 react-native: https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz => 0.63.2 react-native-web: ~0.13.12 => 0.13.18 Expo Workflow: managed
Device info: Samsung Galaxy S21 Ultra 5G / Android 11 / Internal storage only
Reproducible demo or steps to reproduce from a blank project
import * as MediaLibrary from "expo-media-library";
import * as FileSystem from "expo-file-system";
static copyToLibraryAsync = async (localUri) => {
// For example: localUri = FileSystem.cacheDirectory + "image.jpg"
console.log(localUri)
// file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540username%252Fappname/image.jpg
const permissions = await MediaLibrary.getPermissionsAsync();
console.log(permissions); // { canAskAgain: true, expires: "never", granted: true, status: "granted" }
try {
await MediaLibrary.createAssetAsync(localUri)
// await MediaLibrary.saveToLibraryAsync(localUri); // Same error message
} catch (e) {
console.log(e) // [Error: Unable to copy file into external storage.]
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:14 (2 by maintainers)
Top Related StackOverflow Question
To save time of people having the same problem as me, this is my code. I hope this helps someone. Works fine on sdk41 & android 11.
Hi, here is the Snack to reproduce the error on Android 11. With Samsung A12 (Android 11), the .jpeg file is saved successfully, but .xlsx file throws the error.
https://snack.expo.io/@remato/anxious-churros
Any other ideas how to download .xlsx files to user-accessible location would be appreciated, because this is blocking us from using SDK41.
One way to do this on Android 11 without using media-library is using StorageAccessFramework, but this includes so many steps and is too complicated for users: