如何获得的文件在Android目录中的PhoneGap会看到如何获得、文件、目录中、Android

由网友(可爱不动啦)分享简介:我希望能够复制一些文件到我的PhoneGap /科尔多瓦的Documents目录,使他们出现的时候我用的是科尔多瓦 - 插件,文件API列出该目录。不幸的是,似乎是文件API,什么是真正奠定了平板电脑的存储设备之间有些脱节。以下是文件插件规范说,该系统的目录结构应该是这样的:I'd like to be able t...

我希望能够复制一些文件到我的PhoneGap /科尔多瓦的Documents目录,使他们出现的时候我用的是科尔多瓦 - 插件,文件API列出该目录。不幸的是,似乎是文件API,什么是真正奠定了平板电脑的存储设备之间有些脱节。以下是文件插件规范说,该系统的目录结构应该是这样的:

I'd like to be able to copy some files into my PhoneGap / Cordova's Documents directory, so that they show up when I use the cordova-plugin-file API to list that directory. Unfortunately, there seems to be some disconnect between the file API and what's actually laid out on the tablet's storage. Here's what the File plugin spec says the system directory structure should look like:

文件:/// android_asset / | cordova.file.applicationDirectory 文件:/// android_asset /数据/数据​​/&LT; APP-ID&GT; / | cordova.file.applicationStorageDirectory 文件:/// android_asset /数据/数据​​/&LT; APP-ID&GT; /缓存 | cordova.file.cacheDirectory 文件:/// android_asset /数据/数据​​/&LT; APP-ID&GT; /文件 | cordova.file.dataDirectory 文件:/// android_asset /数据/数据​​/&LT; APP-ID&GT; /文件 | cordova.file.documents &LT; SD卡&GT; / | cordova.file.externalRootDirectory &LT; SD卡&GT; /安卓/数据/&LT; APP-ID&GT; / | cordova.file.externalApplicationStorageDirectory &LT; SD卡&GT; /安卓/数据/&LT; APP-ID&GT; /缓存 | cordova.file.externalCacheDirectry &LT; SD卡&GT; /安卓/数据/&LT; APP-ID&GT; /文件 | cordova.file.externalDataDirectory 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。相反,我看到的:

Unfortunately, I'm not seeing this when I plug my device (4.4.2 / Lenovo tablet) into my PC or Mac. Instead, I see:

- 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

任何想法,我应该被复制的文件,使我的应用程序可以看到他们?

Any idea where I should be copying the files so that my app can see them?

推荐答案

现在好了。我的问题之一是,我有点对科尔多瓦文件系统/科尔多瓦 - 插件文件API的异步性。我不得不做一些code重构来获取文件列表正确显示出来,但一旦我做了,这些文件显示正常,无论在哪里,他们的设备上。

Well now. Part of my problem was that I got bit by the asynchronous nature of the filesystem / cordova-plugin-file API on cordova. I had to do some code refactoring to get the file list to show up properly, but once I did, the files displayed properly regardless of where they were on the device.

下面是相应的code。请注意,您所需要的科尔多瓦 - 插件文件添加到您的科尔多瓦/ PhoneGap的项目,它不会在浏览器中运行。其实我有这个区块内的另一个的if / then块 - 如果它在浏览器中运行,显示HTML5 &LT;输入类型=文件&gt; ,如果它在移动设备,表明此块:

Here's the applicable code. Note that you'll need the cordova-plugin-file added to your Cordova/PhoneGap project, and that it won't work in the browser. I actually have this block inside another if/then block -- if it's running in a browser, show the html5 <input type=file>, if it's in a mobile device, show this block:

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);
}
阅读全文

相关推荐

最新文章