问题
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