Phonegap Cordova删除文件(android)

den*_*pey 6 java filesystems cordova

直升机..

我是手机新手......

我有一个问题,删除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);
Run Code Online (Sandbox Code Playgroud)

记录结果

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:此路径中包含无效的":".

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

搜索之后,我在doc中得到了这个(错误代码和含义列表)

5 = ENCODING_ERR

文件路径是错误的以及如何在sdcard中获取文件的有效路径?

谢谢

cor*_*cor 7

我认为你的问题在于你调用回调函数的方式.这段代码对我有用:

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);
});
Run Code Online (Sandbox Code Playgroud)


szu*_*ken 6

访问绝对路径的最简单方法file://是使用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);
Run Code Online (Sandbox Code Playgroud)

其他有用的链接: