如何获取PhoneGap将看到的android目录中的文档
我希望能够将一些文件复制到我的PhoneGap/Cordova的Documents目录中,以便在我使用Cordova插件文件API列出该目录时显示这些文件。不幸的是,在文件API和平板电脑存储的实际布局之间似乎存在一些脱节。以下是文件插件规范所述的系统目录结构: Android文件系统布局如何获取PhoneGap将看到的android目录中的文档,android,cordova,cordova-plugins,Android,Cordova,Cordova Plugins,我希望能够将一些文件复制到我的PhoneGap/Cordova的Documents目录中,以便在我使用Cordova插件文件API列出该目录时显示这些文件。不幸的是,在文件API和平板电脑存储的实际布局之间似乎存在一些脱节。以下是文件插件规范所述的系统目录结构: Android文件系统布局 file:///android_asset/| cordova.file.applicationDirectory file:///android_asset/data/data//| cordova.fi
| cordova.file.applicationDirectoryfile:///android_asset/
| cordova.file.applications存储目录file:///android_asset/data/data//
| cordova.file.cacheDirectoryfile:///android_asset/data/data//cache
| cordova.file.dataDirectoryfile:///android_asset/data/data//files
| cordova.file.documentsfile:///android_asset/data/data//Documents
| cordova.file.externalRootDirectory/
| cordova.file.externalapplications存储目录/Android/data/
| cordova.file.externalCacheDirectry/Android/data//cache
| cordova.file.externalDataDirectory/Android/data//files
- 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
你知道我应该在哪里复制文件以便我的应用程序可以看到它们吗?好了。我的部分问题是,我被cordova上文件系统/cordova插件文件API的异步特性所困扰。我必须进行一些代码重构以使文件列表正确显示,但一旦我进行了重构,文件就会正确显示,无论它们在设备上的什么位置 这是适用的代码。请注意,您需要将cordova插件文件添加到cordova/PhoneGap项目中,并且它在浏览器中不起作用。我实际上在另一个if/then块中有这个块——如果它在浏览器中运行,则显示html5
,如果它在移动设备中,则显示此块:
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);
}
然后,最好是在应用程序启动代码的某个地方(即设备就绪事件的处理程序),检查运行时权限,并在需要时添加它们:
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);
});
}
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);
});
}