4

我正在构建一个 Phonegap/Cordova 应用程序,它下载一些文件并将它们保存在设备上。为此,我使用 File API。

window.requestFileSystem(LocalFileSystem.PERSISTENT,
    0,
    function (fileSystem) {
        rootPath = fileSystem.root.fullPath;
    }, 
    fail
);

在 iOS 上,这将设置rootPath为应用程序的私有目录,这很好。在 Android 上,这将设置rootPath为外部存储的根目录,这有点问题,因为这些文件不绑定到应用程序,并且在删除应用程序时不会被删除。据我了解,在 Android 上执行此操作的正确方法是使用getExternalFilesDir. 如何getExternalFilesDir通过Phonegap获得功能?

4

2 回答 2

4

您想通过 JS 请求外部文件目录。

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
    function (fileSystem) {
        fileSystem.root.getDirectory("Android/data/com.my.app/files", 
            {create: true, exclusive: false}, 
            function(dirEntry) {
                rootPath = dirEntry.fullPath;
            }, fail);;
    }, 
    fail
);

现在,您有一个指向将在卸载应用程序时清理的区域的路径。

于 2013-02-14T16:52:12.267 回答
0

您无法跟踪卸载事件。但要删除目录,代码如下。

function deleteDirectory(directoyName){
    $scope.fs.root.getDirectory(directoyName,{ create: false, exclusive: false }, function(dirEntry) {
      dirEntry.removeRecursively(function() {
        console.log('Directory Successfully Removed.');
      }, errorHandler);
    }, errorHandler);
  }
于 2015-07-20T06:26:06.560 回答