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

偶尔善良 提交于 2019-12-11 03:18:57

问题


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.


回答1:


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);
  }


来源:https://stackoverflow.com/questions/54978453/possible-unhandled-promise-rejection-id-0-error-permission-denied-while-sa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!