如何在PhoneGap将看到的android目录中获取文档

eb1*_*eb1 5 android cordova cordova-plugins

我希望能够将一些文件复制到我的PhoneGap/Cordova的Documents目录中,以便在我使用cordova-plugin-file API列出该目录时显示它们.不幸的是,文件API与平板电脑存储上实际存在的内容之间似乎存在一些脱节.以下是File插件规范所说的系统目录结构应如下所示:

Android文件系统布局

  • file:///android_asset/| cordova.file.applicationDirectory
  • file:///android_asset/data/data/<app-id>/| cordova.file.applicationStorageDirectory
  • file:///android_asset/data/data/<app-id>/cache| cordova.file.cacheDirectory
  • file:///android_asset/data/data/<app-id>/files| cordova.file.dataDirectory
  • file:///android_asset/data/data/<app-id>/Documents| cordova.file.documents
  • <sdcard>/| cordova.file.externalRootDirectory
  • <sdcard>/Android/data/<app-id>/| cordova.file.externalApplicationStorageDirectory
  • <sdcard>/Android/data/<app-id>/cache| cordova.file.externalCacheDirectry
  • <sdcard>/Android/data/<app-id>/files| cordova.file.externalDataDirectory

不幸的是,当我将设备(4.4.2 /联想平板电脑)插入我的PC或Mac时,我没有看到这一点.相反,我看到:

- Internal Storage
|- .IdeaDesktopHD
|- .lelauncher
|- .magic
|- .powercenterhd
|- Alarms
|- Android
|- Audio
|- Bluetooth
|- Contact
|- data
|- DCIM
|- Document
|- Download
|- googleota
|- legc
|- LenovoReaper
|- LesyncDownload
|- Movies
|- MyFavorite
|- Notifications
|- Others
|- Pictures
|- Podcasts
|- powercenterhd
|- Ringtones
|- SHAREit
Run Code Online (Sandbox Code Playgroud)

知道我应该在哪里复制文件,以便我的应用程序可以看到它们吗?

eb1*_*eb1 11

现在好了.我的部分问题是我对cordova上的filesystem/cordova-plugin-file API的异步性质有所了解.我不得不做一些代码重构以使文件列表正确显示,但是一旦我这样做,文件就会正常显示,无论它们在设备上的位置如何.

这是适用的代码.请注意,您需要将cordova-plugin文件添加到Cordova/PhoneGap项目中,并且它在浏览器中不起作用.我实际上在另一个if/then块中有这个块 - 如果它在浏览器中运行,则显示html5 <input type=file>,如果它在移动设备中,则显示此块:

var localURLs    = [
    cordova.file.dataDirectory,
    cordova.file.documentsDirectory,
    cordova.file.externalApplicationStorageDirectory,
    cordova.file.externalCacheDirectory,
    cordova.file.externalRootDirectory,
    cordova.file.externalDataDirectory,
    cordova.file.sharedDirectory,
    cordova.file.syncedDataDirectory
];
var index = 0;
var i;
var statusStr = "";
var addFileEntry = function (entry) {
    var dirReader = entry.createReader();
    dirReader.readEntries(
        function (entries) {
            var fileStr = "";
            var i;
            for (i = 0; i < entries.length; i++) {
                if (entries[i].isDirectory === true) {
                    // Recursive -- call back into this subdirectory
                    addFileEntry(entries[i]);
                } else {
                   fileStr += (entries[i].fullPath + "<br>"); // << replace with something useful
                   index++;
                }
            }
            // add this directory's contents to the status
            statusStr += fileStr;
            // display the file list in #results
            if (statusStr.length > 0) {
                $("#results").html(statusStr);
            } 
        },
        function (error) {
            console.log("readEntries error: " + error.code);
            statusStr += "<p>readEntries error: " + error.code + "</p>";
        }
    );
};
var addError = function (error) {
    console.log("getDirectory error: " + error.code);
    statusStr += "<p>getDirectory error: " + error.code + ", " + error.message + "</p>";
};
for (i = 0; i < localURLs.length; i++) {
    if (localURLs[i] === null || localURLs[i].length === 0) {
        continue; // skip blank / non-existent paths for this platform
    }
    window.resolveLocalFileSystemURL(localURLs[i], addFileEntry, addError);
}
Run Code Online (Sandbox Code Playgroud)

编辑(2018年2月):即使您在Android文件传输中看到文件,即使您在Cordova应用程序中设置了构建时权限,也可能无法以编程方式返回任何结果.这是由于添加到Android的运行时权限检查(我相信> 6.0).有几个插件可以帮助解决这个问题; 在某些时候,我猜测文件插件也会为它添加自动请求.这是我在科尔多瓦做过的事情cli-7.0.1:

在config.xml中,设置所需的应用程序权限.你需要READ_EXTERNAL_STORAGE(如果你打算这样做,也要写.)我还添加了两个插件,如下所示:

<plugin name="cordova-plugin-device" source="npm" spec="1.1.6" />
<plugin name="cordova.plugins.diagnostic" spec="^3.7.1" />

<config-file platform="android" parent="/manifest" mode="replace">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>
Run Code Online (Sandbox Code Playgroud)

然后,最好在应用程序的启动代码中的某个位置(即设备就绪事件的处理程序),检查运行时权限并在需要时添加它们:

if (device.platform === "Android") {
    // request read access to the external storage if we don't have it
    cordova.plugins.diagnostic.getExternalStorageAuthorizationStatus(function (status) {
        if (status === cordova.plugins.diagnostic.permissionStatus.GRANTED) {
            console.log("External storage use is authorized");
        } else {
            cordova.plugins.diagnostic.requestExternalStorageAuthorization(function (result) {
                console.log("Authorization request for external storage use was " + (result === cordova.plugins.diagnostic.permissionStatus.GRANTED ? "granted" : "denied"));
            }, function (error) {
                console.error(error);
            });
        }
    }, function (error) {
        console.error("The following error occurred: " + error);
    });
}
Run Code Online (Sandbox Code Playgroud)