我必须从设备中的SD卡读取数据。
我首先尝试通过cordova-plugin-file进行操作,但这只是显示file:///storage/emulated/0/
而不是sdcard目录。
我发现我需要使用cordova.plugins.diagnostic来解决问题,但是当我使用它(与Diagnostic.getExternalSdCardDetails().then(OnSucceedFunction, OnFailureFunction)
*一起使用时)可以在网络视图上“工作”(向我发送错误cordova_not_available
,但这是逻辑),但是在android应用中,应用上没有任何追加内容,因此我需要使用android控制台来查看:
E/Diagnostic: Exception occurred onRequestPermissionsResult: No context found for request id=1000
java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.error(java.lang.String)' on a null object reference
我在堆栈溢出或互联网上的任何地方都找不到任何东西,所以,我在这里寻求帮助
* OnSucceedFunction和OnFailureFunction只是为了让您了解我在做什么,我知道如果我编写它是行不通的
答案 0 :(得分:1)
您需要离子性Diagnostic
ionic cordova plugin add cordova-plugin-file
npm install @ionic-native/file
根据@MarcusIII(他的回答)this
this.diagnostic.requestExternalStorageAuthorization().then(()=>{
//your permission
}).catch(error=>{
//Error Handling part
});
答案 1 :(得分:0)
我认为,如果您需要按照文档阅读SDCard,则需要像这样修改您的代码
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + 'file_name', readFile, onErrorReadFile);
function readFile(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function() {
console.log("Successful file read: " + this.result);
displayFileData(fileEntry.fullPath + ": " + this.result);
};
reader.readAsText(file);
}, onErrorReadFile);
}
function onErrorReadFile(error) {
console.log('error reading', error);
}
cordova.file.externalRootDirectory将帮助您指向SD卡。