我搜索了检查/请求权限,发现了 2 个插件:
第一:安卓权限
https://ionicframework.com/docs/native/android-permissions/
第二:诊断
https://ionicframework.com/docs/native/diagnostic/
我检查了第二个,但当我写EXTERNAL_STORAGE时它显示错误。然后我检查了第一个,但它不起作用.这是我的代码:
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then( (result) => {
//console.log('Has permission?',result.hasPermission)
},err => {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(_=>{
targetPath = cordova.file.externalRootDirectory+ "download/"+moment().format("YYYYMMDDHHmmsss")+".jpg";
fileTransfer.download(image.img, targetPath, true).then((entry) => {
alert('download complete: ' + entry.toURL());
}, (error) => {
alert("please check application permissions");
});
});
});
那么哪个插件在离子中工作以及如何工作?
通过使用
第一个插件解决了问题,这样:
download(image) {
const fileTransfer: FileTransferObject = this.transfer.create();
let targetPath = cordova.file.externalRootDirectory+ "download/"+moment().format("YYYYMMDDHHmmsss")+".jpg";
fileTransfer.download(image.img, targetPath, true).then((entry) => {
alert('download complete: ' + entry.toURL());
}, (error) => {
this.checkPermissions();
});
}
checkPermissions(){
this.androidPermissions.requestPermissions(
[
this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
]
);
}