$cordovaFile removeFile & checkDir error 5 (ENCODI

2019-08-11 21:46发布

问题:

I am trying to use $cordovaFile to delete files from an Android device. The file to be deleted was downloaded using $cordovaFileTransfer in the following location.

ft.download(url, cordova.file.externalDataDirectory + "episodes/" + episodeId + ".mp3",...);

Using the file manager on the device I can see that the file is located at file:///storage/emulated/0/Android/data/com.ionicframework.myapp123456/files/episodes/

`However when I try to use either of the following locations to delete the file I get error code 5 (ENCODING_ERR)

$cordovaFile.removeFile("file:///storage/emulated/0/Android/data/com.ionicframework.myapp123456/files/episodes/",  "0.mp3")

$cordovaFile.removeFile(cordova.file.externalDataDirectory + "episodes/",  "0.mp3")

That same error occurs when I try to use $cordovaFile.checkDir() so I used window.resolveLocalFileSystemURL() instead.

回答1:

This issue was resolved by using the LocalFileSystem. It is not as elegent as the methods provided by the file plugin, but it works.

var uri = cordova.file.externalDataDirectory + "episodes/";

window.resolveLocalFileSystemURL(uri, function(dir) {
        dir.getFile(episodeId + ".mp3", { create: false }, function(file) {
            file.remove(successCallback, errorCallback);
        });
}, errorCallback);