Cordova Diagnostic Exception on Diagnostic.getExternalSdCardDetails()

Viewed 438

I have to read data from an SD card in a device.

I first try to do it via cordova-plugin-file, but it just show me file:///storage/emulated/0/ and not the sdcard directory.

I found out that I needed to use cordova.plugins.diagnostic to solve my problem, but when I use it (with Diagnostic.getExternalSdCardDetails().then(OnSucceedFunction, OnFailureFunction)*), it "work" on on web view (by send me the error cordova_not_available, but it's logic), but in android app, nothing append on the app, and I need to use android console to see:

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

and i found nothing on stack overflow or anywhere on internet, so, I'm here to ask for some help

*OnSucceedFunction and OnFailureFunction are just to make you understand what am I doing, I know if I write it it won't work

2 Answers

you need ionic 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
});

I think if you need to read from SDCard, as per the documentation, your code needs to be modified like this

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 will help you point to your SDCard.

Related