Fazle Rabbi Ador
Fazle Rabbi Ador

Reputation: 887

Possible Unhandled Promise Rejection (id: 0): Error: Permission denied (while saving the image via "CameraRoll.saveToCameraRoll()")

ss from my debugger

I am getting Permission denied when trying to save the image to the gallery via "CameraRoll.saveToCameraRoll()" from react native. my code is below==>

takePicture = async function() {
if (this.camera) {
  const data = await this.camera.takePictureAsync();
  let saveResult = CameraRoll.saveToCameraRoll(data.uri);
  console.warn('takePicture ', saveResult);
  console.warn('picture url ', data.uri);
}

};

i have taken permissions from android manifest and ios by adding required codes. (On Android) android manifest permissions

even in ios i am getting the same permission denied error. what should i do to work it. i don't wanna try RN File system.

Upvotes: 0

Views: 4816

Answers (2)

Abiodun Sulaiman
Abiodun Sulaiman

Reputation: 1

After adding the permissions in runtime, the problem persisted until I did the below:

Add the code below into application in android/app/src/main/AndroidManifesst.xml:

<application
android:requestLegacyExternalStorage="true"
...

Upvotes: 0

Fazle Rabbi Ador
Fazle Rabbi Ador

Reputation: 887

i have gone through lots of resources and finally got the reason. Somehow my manifest permission didn't get the permission for writing external permission as below.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

so what i did i added runtime permission for the external writing and it works fine. i use PermissionsAndroid from react-native.

codes are below =>

try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
      {
        title: "Cool Photo App Camera Permission",
        message:
          "Cool Photo App needs access to your camera " +
          "so you can take awesome pictures.",
        buttonNeutral: "Ask Me Later",
        buttonNegative: "Cancel",
        buttonPositive: "OK"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the camera");

      const data = await this.camera.takePictureAsync();
      let saveResult = CameraRoll.saveToCameraRoll(data.uri);
      console.warn("takePicture ", saveResult);
      console.warn("picture url ", data.uri);
    } else {
      console.log("Camera permission denied");
    }
  } catch (err) {
    console.warn(err);
  }

Upvotes: 1

Related Questions