I want to save the images in the cameraRoll and I am using @react-native-community/cameraroll. To install the package I ran:
yarn add @react-native-community/cameraroll
in settings.gradle file I added:
include ':@react-native-community_cameraroll'
project(':@react-native-community_cameraroll').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/cameraroll/android')
I ran
yarn add fbjs
because I had the error in this link: https://github.com/react-native-cameraroll/react-native-cameraroll/issues/352
I added these permissions in AndroidManifest.xml file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
and I added android:requestLegacyExternalStorage="true" in the application tag of AndroidManifest
This is my code:
import CameraRoll from "@react-native-community/cameraroll";
async function hasAndroidPermission() {
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
const hasPermission = await PermissionsAndroid.check(permission);
if (hasPermission) {
return true;
}
const status = await PermissionsAndroid.request(permission);
return status === 'granted';
}
const savePicture = async (path,album)=>{
if (Platform.OS === "android" && !(await hasAndroidPermission())) {
return;
}
CameraRoll.save(path, {album})
}
When I call savePicture I have this warning:
Possible Unhandled Promise Rejection (id: 0):
Error: Operation not permitted
When I test this in android 9/10 I have no problems. With android 11/12 I have the warning and the pictures doesn't save. React-native version: 0.68.2
Someone can help me please? Thanks in advance.
Use this library for permission in react native react-native-permissions. It will work on both android and iOS.