CordovaResourceApi不支持URI

时间:2014-12-23 09:36:47

标签: android cordova file-transfer

我正在尝试下载文件并在应用的 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

3 个答案:

答案 0 :(得分:2)

AFAIK您无法在www文件夹中下载任何内容。那是不允许的。

答案 1 :(得分:1)

不确定OP是否已经解决了这个问题,不幸的是" applicationDirectory"是只读的。
以下是我在cordovaFile插件网页上找到的更多信息:
(参考:https://github.com/apache/cordova-plugin-file
不知道如何在这里插入大表,所以我把它作为一个片段,只需运行它就可以看到表格,或者你可以直接进入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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

  

参考:Android File System Layout