首页 > 解决方案 > 如何在 Cordova android 应用程序的根目录而不是应用程序数据目录中写入文件?

问题描述

我正在构建一个 Cordova android 应用程序并用于cordova-plugin-file在本地系统上写入文件。我正在使用的代码如下:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {

    console.log('file system open: ' + fs.name);
    fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {

        console.log("fileEntry is file?" + fileEntry.isFile.toString());
        writeFile(fileEntry, null);

    }, onErrorCreateFile);

}, onErrorLoadFs);

该文件newPersistentFile.txt将在app数据目录中创建。如何将文件写入系统根级目录,例如/Downloads,Notifications等?

标签: androidcordova

解决方案


您想查看 cordova.file 目录(请参阅docs)。

所以对于下载:

window.requestFileSystem(cordova.file.externalRootDirectory + "Downloads", 0, function(fs) {
   console.log('file system open: ' + fs.name);
   ...
}

推荐阅读