【问题标题】:URI not supported by CordovaResourceApiCordovaResourceApi 不支持 URI
【发布时间】:2017-02-28 18:55:12
【问题描述】:

我正在尝试下载一个文件并将其保存(如果是这种情况,将其覆盖)到我的应用程序的 www 文件夹中。

你能指导我一些代码示例吗?

这是我尝试过的代码:

document.addEventListener("deviceready", init, false);

var store2;

var assetURL = "http://www.mydomain.eu/path/untitled.png";

var fileName = "bg.png";

function init() {
    try {

       store2 = cordova.file.applicationDirectory + 'www/';              

    }
    catch (err) {
        alert("Error: " + err.message);
    }
}

function downloadAsset2() {

    try {
        var fileTransfer = new FileTransfer();
        alert("aa->" + store2 + fileName)
        fileTransfer.download(assetURL, store2 + fileName,
            function (entry) {

                location.reload();
                alert("->" + store2 + fileName)
            },
            function (err) {                
                alert(JSON.stringify(err, null, 4));
            });
    }
    catch (err) {
        alert("Error: " + err.message);
    }
}

这是我得到的错误

URI not supported by CordovaResourceApi: file:///android_asset/www/bg.png

【问题讨论】:

  • 我猜这些资产是只读的。
  • hmm.. 这可能是因为如果我使用 cordova.file.dataDirectory 路径,这段代码可以正常工作,但我不确定..

标签: android cordova file-transfer


【解决方案1】:

AFAIK 你不能在www 文件夹中下载任何东西。这是不允许的。

【讨论】:

    【解决方案2】:

    不确定 OP 是否已经解决了这个问题,不幸的是“applicationDirectory”是只读的。
    以下是我从 cordovaFile 插件网页中找到的更多信息:
    (参考:https://github.com/apache/cordova-plugin-file
    不知道如何在此处插入大表,所以我将其设置为 sn-p,只需运行它即可看到该表,或者您可以直接转到 gitHub 页面了解更多信息

    <h3><a id="user-content-ios-file-system-layout" class="anchor" href="#ios-file-system-layout" aria-hidden="true"><svg aria-hidden="true" class="octicon octicon-link" height="16" role="img" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1h-1c-1.5 0-3-1.69-3-3.5s1.55-3.5 3-3.5h4c1.45 0 3 1.69 3 3.5 0 1.41-0.91 2.72-2 3.25v-1.16c0.58-0.45 1-1.27 1-2.09 0-1.28-1.02-2.5-2-2.5H4c-0.98 0-2 1.22-2 2.5s1 2.5 2 2.5z m9-3h-1v1h1c1 0 2 1.22 2 2.5s-1.02 2.5-2 2.5H9c-0.98 0-2-1.22-2-2.5 0-0.83 0.42-1.64 1-2.09v-1.16c-1.09 0.53-2 1.84-2 3.25 0 1.81 1.55 3.5 3 3.5h4c1.45 0 3-1.69 3-3.5s-1.5-3.5-3-3.5z"></path></svg></a>iOS File System Layout</h3>
    
    <table>
      <thead>
        <tr>
          <th align="left">Device Path</th>
          <th align="left"><code>cordova.file.*</code>
          </th>
          <th align="left"><code>iosExtraFileSystems</code>
          </th>
          <th align="center">r/w?</th>
          <th align="center">persistent?</th>
          <th align="center">OS clears</th>
          <th align="center">sync</th>
          <th align="center">private</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td align="left"><code>/var/mobile/Applications/&lt;UUID&gt;/</code>
          </td>
          <td align="left">applicationStorageDirectory</td>
          <td align="left">-</td>
          <td align="center">r</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>appname.app/</code>
          </td>
          <td align="left">applicationDirectory</td>
          <td align="left">bundle</td>
          <td align="center">r</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>www/</code>
          </td>
          <td align="left">-</td>
          <td align="left">-</td>
          <td align="center">r</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>Documents/</code>
          </td>
          <td align="left">documentsDirectory</td>
          <td align="left">documents</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>NoCloud/</code>
          </td>
          <td align="left">-</td>
          <td align="left">documents-nosync</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>Library</code>
          </td>
          <td align="left">-</td>
          <td align="left">library</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">Yes?</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>NoCloud/</code>
          </td>
          <td align="left">dataDirectory</td>
          <td align="left">library-nosync</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>Cloud/</code>
          </td>
          <td align="left">syncedDataDirectory</td>
          <td align="left">-</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>Caches/</code>
          </td>
          <td align="left">cacheDirectory</td>
          <td align="left">cache</td>
          <td align="center">r/w</td>
          <td align="center">Yes*</td>
          <td align="center">Yes***</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>tmp/</code>
          </td>
          <td align="left">tempDirectory</td>
          <td align="left">-</td>
          <td align="center">r/w</td>
          <td align="center">No**</td>
          <td align="center">Yes***</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
      </tbody>
    </table>
    
    <p>* Files persist across app restarts and upgrades, but this directory can be cleared whenever the OS desires. Your app should be able to recreate any content that might be deleted.</p>
    
    <p>** Files may persist across app restarts, but do not rely on this behavior. Files are not guaranteed to persist across updates. Your app should remove files from this directory when it is applicable, as the OS does not guarantee when (or even if) these
      files are removed.</p>
    
    <p>*** The OS may clear the contents of this directory whenever it feels it is necessary, but do not rely on this. You should clear this directory as appropriate for your application.</p>
    
    <h3><a id="user-content-android-file-system-layout" class="anchor" href="#android-file-system-layout" aria-hidden="true"><svg aria-hidden="true" class="octicon octicon-link" height="16" role="img" version="1.1" viewBox="0 0 16 16" width="16"><path d="M4 9h1v1h-1c-1.5 0-3-1.69-3-3.5s1.55-3.5 3-3.5h4c1.45 0 3 1.69 3 3.5 0 1.41-0.91 2.72-2 3.25v-1.16c0.58-0.45 1-1.27 1-2.09 0-1.28-1.02-2.5-2-2.5H4c-0.98 0-2 1.22-2 2.5s1 2.5 2 2.5z m9-3h-1v1h1c1 0 2 1.22 2 2.5s-1.02 2.5-2 2.5H9c-0.98 0-2-1.22-2-2.5 0-0.83 0.42-1.64 1-2.09v-1.16c-1.09 0.53-2 1.84-2 3.25 0 1.81 1.55 3.5 3 3.5h4c1.45 0 3-1.69 3-3.5s-1.5-3.5-3-3.5z"></path></svg></a>Android File System Layout</h3>
    
    <table>
      <thead>
        <tr>
          <th align="left">Device Path</th>
          <th align="left"><code>cordova.file.*</code>
          </th>
          <th align="left"><code>AndroidExtraFileSystems</code>
          </th>
          <th align="center">r/w?</th>
          <th align="center">persistent?</th>
          <th align="center">OS clears</th>
          <th align="center">private</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td align="left"><code>file:///android_asset/</code>
          </td>
          <td align="left">applicationDirectory</td>
          <td align="left"></td>
          <td align="center">r</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"><code>/data/data/&lt;app-id&gt;/</code>
          </td>
          <td align="left">applicationStorageDirectory</td>
          <td align="left">-</td>
          <td align="center">r/w</td>
          <td align="center">N/A</td>
          <td align="center">N/A</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>cache</code>
          </td>
          <td align="left">cacheDirectory</td>
          <td align="left">cache</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">Yes*</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>files</code>
          </td>
          <td align="left">dataDirectory</td>
          <td align="left">files</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"> <code>Documents</code>
          </td>
          <td align="left"></td>
          <td align="left">documents</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">Yes</td>
        </tr>
        <tr>
          <td align="left"><code>&lt;sdcard&gt;/</code>
          </td>
          <td align="left">externalRootDirectory</td>
          <td align="left">sdcard</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">No</td>
        </tr>
        <tr>
          <td align="left"> <code>Android/data/&lt;app-id&gt;/</code>
          </td>
          <td align="left">externalApplicationStorageDirectory</td>
          <td align="left">-</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">No</td>
        </tr>
        <tr>
          <td align="left"> <code>cache</code>
          </td>
          <td align="left">externalCacheDirectry</td>
          <td align="left">cache-external</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No**</td>
          <td align="center">No</td>
        </tr>
        <tr>
          <td align="left"> <code>files</code>
          </td>
          <td align="left">externalDataDirectory</td>
          <td align="left">files-external</td>
          <td align="center">r/w</td>
          <td align="center">Yes</td>
          <td align="center">No</td>
          <td align="center">No</td>
        </tr>
      </tbody>
    </table>
    
    <p>* The OS may periodically clear this directory, but do not rely on this behavior. Clear the contents of this directory as appropriate for your application. Should a user purge the cache manually, the contents of this directory are removed.</p>
    
    <p>** The OS does not clear this directory automatically; you are responsible for managing the contents yourself. Should the user purge the cache manually, the contents of the directory are removed.</p>
    
    <p><strong>Note</strong>: If external storage can't be mounted, the <code>cordova.file.external*</code>
      properties are <code>null</code>.</p>

    【讨论】:

      【解决方案3】:

      使用store2 = cordova.file.dataDirectory ; 这个是指android中的files,它是R/W(允许读写)。

      参考:Android File System Layout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        • 1970-01-01
        • 2017-12-12
        • 2011-05-04
        • 1970-01-01
        相关资源
        最近更新 更多