MediaLibrary.saveToLibraryAsync Error: Unable to copy file into external storage

Solution for MediaLibrary.saveToLibraryAsync Error: Unable to copy file into external storage
is Given Below:

I’m attempting to save a video to my cameraRoll in React Native on Android, console returns the video uri perfectly fine, but I get the error.

Error: Unable to copy file into external storage.

However, when putting the code on Snack, it works fine. Just not when I do a

react-native run-android

Here is the console logs

[Mon Aug 02 2021 21:03:30.108]  LOG      Video source:  file:///data/user/0/com.allies.ai/cache/Camera/74eb667a-a799-4f4e-8d80-98261580c36e.mp4
[Mon Aug 02 2021 21:03:32.222]  WARN     Possible Unhandled Promise Rejection (id: 0):
Error: Unable to copy file into external storage.
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:4552:45
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:109931:40
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:228365:95
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25289:23
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25462:32
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25289:23
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25362:30
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25392:19
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27386:9
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27550:25
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27409:14
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25391:33
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25396:157
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25413:69
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:228352:38
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:228022:48
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25289:23
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25462:32
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25289:23
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25362:30
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:25372:21
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27377:16
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27478:27
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:30917:17
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:30956:17
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31173:33
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5034:35
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:4820:34
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5017:15
[email protected]://localhost:8081/index.bundle?platform=android&dev=true&minify=false:4819:21
[email protected][native code]
[email protected][native code]

Here is the code on Snack:

https://snack.expo.dev/@ptang/camera

Thanks in Advance.

So for those with the similiar issue – Android 10+ now uses what’s called Scope Storage. In short, there are restrictions on saving to your camera roll, there isn’t any work around at this time of the answer for expo-camera OR react-native-cameraroll.

The only temporary fix is to add this following line:

android:requestLegacyExternalStorage="true"

to your AndroidManifest.xml file, specifically in the activity section:

<manifest>
... 
 <application>
...
   <activity android:name=".MainActivity" 
   android:requestLegacyExternalStorage="true">
...
   </activity>
...
 </application>
...
</manifest>