可能的未处理的承诺拒绝(id:0):错误:权限被拒绝(通过"CameraRoll.saveToCameraRoll()"保存图像时) [英] Possible Unhandled Promise Rejection (id: 0): Error: Permission denied (while saving the image via "CameraRoll.saveToCameraRoll()")
问题描述
当我尝试通过React Native通过"CameraRoll.saveToCameraRoll()"将图像保存到图库时,我的权限被拒绝.我的代码在下面==>
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);
}
};
我已通过添加所需的代码从android清单和ios获得许可. (在Android上) Android清单权限
i have taken permissions from android manifest and ios by adding required codes. (On Android) android manifest permissions
即使在ios中,我也遇到相同的权限被拒绝错误.我应该怎么做才能工作.我不想尝试RN文件系统.
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.
推荐答案
我经历了很多资源,终于找到了原因.不知何故,我的清单权限未获得编写外部权限的权限,如下所示.
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" />
所以我做了什么,我为外部编写添加了运行时权限,并且运行良好.我使用本机中的 PermissionsAndroid .
so what i did i added runtime permission for the external writing and it works fine. i use PermissionsAndroid from react-native.
代码在下面=>
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);
}
这篇关于可能的未处理的承诺拒绝(id:0):错误:权限被拒绝(通过"CameraRoll.saveToCameraRoll()"保存图像时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!