获取iOS phonegap/cordova临时文件夹路径并删除内容

3

我很难理解phonegap/cordova的file api文档。

如何找到应用程序临时文件夹的路径,然后列出/删除内容而不删除文件夹本身?

这与从设备图库中拉取照片时创建临时图像文件的删除有关。

2个回答

7
该函数使用cordova文件插件从tmp文件夹中删除特定文件。
deleteFile: function(fileName) {

        var that = this;

        if (!fileName) {
            console.error("No fileName specified. File could not be deleted.");
            return false;
        }

        window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fileSystem){ // this returns the tmp folder 

            // File found
            fileSystem.root.getFile(fileName, {create: false}, function(fileEntry){
                fileEntry.remove(function(success){
                    console.log(success);
                }, function(error){
                    console.error("deletion failed: " + error);
                });
            }, that.get('fail'));
        }, this.get('fail'));

    }

您可以稍微调整一下,先查找所有文件,然后再删除它们。类似这样的操作:
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fileSystem){ {
    var reader = fileSystem.root.createReader();
    reader.readEntries(function(entries) {

        var i;
        for (i = 0; i < entries.length; i++) {
            if (entries[i].name.indexOf(".png") != -1) {
               // delete stuff from above could go in here
            }
        }
    }, fail);    
 }

0

如果你想删除临时图片,你可以使用清理选项

navigator.camera.cleanup( cameraSuccess, cameraError );

1
这确实可以工作,但是我遇到了这里所概述的问题 https://dev59.com/VmUq5IYBdhLWcg3wMNgK ,我也似乎无法解决。问题在于在使用camera.cleanup方法之后选择的照片不是你选择的那一张,而是最近删除的图片中最新的,而不是你刚刚从相机胶卷中选择的那一张。 - OliverJ90

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接