Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Cordova Delete File (android)

Helo..

I'm new in phonegap..

I had a problem to delete file in android phonegap 3.4

console.log(photo);    

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
  function onFileSystemSuccess(fileSystem) {
    fileSystem.root.getFile(
      photo, {create: false},
      function gotFileEntry(fileEntry) {
        fileEntry.remove();
      },
      onError);
  },
  onError);

Log Result

04-24 16:29:54.234: I/Web Console(16213): file:///storage/sdcard0/DCIM/Camera/1398331773136.jpg

04-24 16:49:01.989: W/System.err(18864): org.apache.cordova.file.EncodingException: This path has an invalid ":" in it.

04-24 16:49:01.994: W/System.err(18864): at org.apache.cordova.file.LocalFilesystem.getFileForLocalURL(LocalFilesystem.java:159)

04-24 16:49:01.994: W/System.err(18864): at org.apache.cordova.file.FileUtils.getFile(FileUtils.java:698)

04-24 16:49:03.664: I/Web Console(18864): 5

after search, i got this (List of Error Codes and Meanings) in doc

5 = ENCODING_ERR

is file path is wrong and how to get valid path to file in sdcard ?

Thanks

like image 734
denapey Avatar asked Apr 24 '14 10:04

denapey


2 Answers

I think your problem is in the way you are calling to the callback functions. This code is working for me:

console.log("remove file");
var relativeFilePath = "MyDir/file_name.png";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
    fileSystem.root.getFile(relativeFilePath, {create:false}, function(fileEntry){
        fileEntry.remove(function(file){
            console.log("File removed!");
        },function(){
            console.log("error deleting the file " + error.code);
            });
        },function(){
            console.log("file does not exist");
        });
    },function(evt){
        console.log(evt.target.error.code);
});
like image 141
cor Avatar answered Nov 10 '22 20:11

cor


The simplest way to access an absolute path starting with file:// is by using window.resolveLocalFileSystemURL()

var url = "file:///storage/emulated/0/Android/data/myPackageName/cache/1461244585881.jpg";

window.resolveLocalFileSystemURL(url, function(file) {
        file.remove(function(){
          console.log(url + " deleted");
        },onError);
      }, onError);

additional helpful links:

  • Apache Cordova File Doc
  • read/write permissions + file system layout:
    • Andriod
    • IOS
    • Windows
like image 20
szuuuken Avatar answered Nov 10 '22 20:11

szuuuken