如何获得的文件在Android目录中的PhoneGap会看到 [英] How to get documents in an android directory that PhoneGap will see

查看:237
本文介绍了如何获得的文件在Android目录中的PhoneGap会看到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够复制一些文件到我的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 /数据/数据​​/< APP-ID> / | cordova.file.applicationStorageDirectory
  • 文件:/// android_asset /数据/数据​​/< APP-ID> /缓存 | cordova.file.cacheDirectory
  • 文件:/// android_asset /数据/数据​​/< APP-ID> /文件 | cordova.file.dataDirectory
  • 文件:/// android_asset /数据/数据​​/< APP-ID> /文件 | cordova.file.documents
  • < SD卡> / | cordova.file.externalRootDirectory
  • < SD卡> /安卓/数据/< APP-ID> / | cordova.file.externalApplicationStorageDirectory
  • < SD卡> /安卓/数据/< APP-ID> /缓存 | cordova.file.externalCacheDirectry
  • < SD卡> /安卓/数据/< APP-ID> /文件 | 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);
}

这篇关于如何获得的文件在Android目录中的PhoneGap会看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆