expo: Expo-media-library createAssetAsync calls error in Android 11 even with MediaLibrary Permission.
Summary
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.]
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 23 (2 by maintainers)
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.
This is because the Media library is able to save image/video/audio assets so it will fail with other file types. This is a bit more complicated if you want to save some documents (pdf/csv etc.) to the downloads folder. Actually I worked around it like this:
However, there is one disadvantage:
Hi,
I have exactly the same issue ! 👎 I download a file then when I call createAssetAsync I have the following error : Unable to copy file into external storage.
Thanks in advance for help!
The error is located in CreateAsset.java line 97.
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:
Hello @barthap thanks for the quick response,
I’m trying to save the .xlsx file, its save in the Download/Data folder with a proper name when opening that file gets corrupted.
Reproducable example : snack expo Code:
Thank you soo much, this worked for me!
I have the same problem @durgesh2007 did you find any solution
Is there any progress with this? I’ve updated to expo 42 and the issue is still here.
Hi- can someone share a Snack link that we can use to reproduce this error?