saveToCameraRoll在Android上出现Permission denied
错误失败
将AWS S3照片保存到设备相机胶卷突然失败
RNFS.downloadFile(DownloadFileOptions)
.promise
.then(() => {
CameraRoll.saveToCameraRoll(path, this.state.type) // path = /data/user/0/com.XXX.app/filesimage-xyz.jpg"
.then((data) => {
RNFS.unlink(path)
.then(() => {
console.log('Download and file unlink succeeded');
})
.catch((err) => {
console.error("RNFS unlink error: ", err.message);
});
})
.catch((error) => {
console.error("Save to Cameral roll error: ",error.message);
//error = code:"EUNSPECIFIED"
// framesToPop:1
// message:"Permission denied"
// stack:"Error: Permission denied↵
});
})
.catch((error) => {
console.error("RNFS.downloadFile! error: ", error.message);
});
在android / app / src / main / AndroidManifest.xml中我有
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
少数其他权限。
什么可以导致权限被拒绝错误?在Manifest级别应该做什么来保存到cameraRoll?
答案 0 :(得分:10)
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。您可以使用PermissionAndroid API授予用户外部存储访问权限。
示例:
requestExternalStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'My App Storage Permission',
message: 'My App needs access to your storage ' +
'so you can save your photos',
},
);
return granted;
} catch (err) {
console.error('Failed to request permission ', err);
return null;
}
};
答案 1 :(得分:2)
您可以尝试:
...
let permission = await Expo.Permissions.askAsync(Expo.Permissions.CAMERA_ROLL);
if (permission.status === 'granted') {
await CameraRoll.saveToCameraRoll(fileUrl, "photo");
}