Can't read file in SD card

I tried to read some file that can be stored in SD card. To do this I’m using the file.readAsText() function, where file is Ionic File. While trying to do this I got the following error:

FileError {code: 1, message: “NOT_FOUND_ERR”}

When I searched online I see that a lot people had this problem and that’s probably related to SD access permission. The file is accessed this way:

this.file.readAsText(var1, var2).then(...)

Where var1, the path, is “file:///sdcard/test/” and var2, the file name, is “file.kml”. When this file is not in SD card, everything work as intended.

To fix this I tried to use cordova-plugin-android-permissions, which I didn’t even know if is the best approach, but already solved some problems with other issues in the past:

this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE).then(resp => {
	console.log(resp) // resp = {hasPermission: true}
});

Which oddly gave me true for READ permission, even so I still get the ‘not found’ error when I try to read the file.

MANAGE_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are false though. The problem is that, when I try to request permission with the plugin, I just got a response as if I’m checking for permissions, which also returns false:

// Made a list to request everything related to external storage
let list: string[] = [
	this.androidPermissions.PERMISSION.MANAGE_EXTERNAL_STORAGE,
	this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
	this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
]
return this.androidPermissions.requestPermissions(list).then(res => {
	console.log(res); // res = {hasPermission: false}
});

I also tried to request a single permission, but the result was the same.


Modified config.xml to include:

<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>

I don’t know if this was the case, but now the permission to read and write are true using the permission plugin, even so I still get the errors. I also tried to get the file with window.resolveLocalFileSystemURL(), but returns FileError with code 1, which is another ‘not found’ variant.


Tried with this method mentioned in the forum, but gor the same FileError (code 1).

let url_file = (file_path+fileName).replace("file:///sdcard/", "");

let file_name;
if(url_file.indexOf("content://") !== -1 || url_file.indexOf("file://") !== -1) {
    window.resolveLocalFileSystemURL(url_file, gotFile, fail);
} else {
   let aux = url_file.split("/");
   file_name = aux[aux.length - 1];
   aux.splice(aux.length - 1, 1);
   url_file = aux.join("/");

   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
     fileSystem.root.getDirectory(url_file, { create: false, exclusive: false }, gotDirEntry, fail);
}

function gotDirEntry(dirEntry) {
    dirEntry.getFile(file_name, { create: false, exclusive: false }, gotFile, fail);
}

function gotFile(fileEntry) {
     fileEntry.file(function (file) {
       let reader = new FileReader();
       reader.onloadend = function (evt) {
           console.log(this.result);
       }
       reader.readAsText(file);
    }, fail);
}

function fail(error) {
    console.log(error);
}

Using Ionic 5
Angular 10
Cordova 9

1 Like

Any chance you’d consider using Capacitor’s Filesystem instead?

2 Likes

I saw you comment in the thread I linked, but sadly I still can’t migrate to Capacitor. I have to finish some specific functions in this app version and I have a lot of cordova plugins in the app, but in the next version I’ll be freer and I’ll probably do that. Believe me, if it depended only on me I’d do it first of all.