Diagnostic.getExternalSdCardDetails() 上的 Cordova 诊断异常

Cordova Diagnostic Exception on Diagnostic.getExternalSdCardDetails()

我必须从设备中的 SD 卡读取数据。

我首先尝试通过 cordova-plugin-file 进行操作,但它只显示 file:///storage/emulated/0/ 而不是 sdcard 目录。

我发现我需要使用 cordova.plugins.diagnostic 来解决我的问题,但是当我使用它时(使用 Diagnostic.getExternalSdCardDetails().then(OnSucceedFunction, OnFailureFunction)*),它 "work" 在网络视图上(通过向我发送错误 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 只是为了让你明白我在做什么,我知道我写了它不会起作用

我认为如果你需要从 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 将帮助您指向您的 SDCard。

你需要离子 Diagnostic

ionic cordova plugin add cordova-plugin-file
npm install @ionic-native/file

According to @MarcusIII (His answer) this

this.diagnostic.requestExternalStorageAuthorization().then(()=>{
//your permission 
}).catch(error=>{
//Error Handling part
});