android - PhoneGap FileTransfer.download 期望的路径与 FileSystem 提供的路径不同

标签 android cordova phonegap-plugins file-transfer android-file

我正在将文件下载到本地文件系统。我可以通过 fileSystem.root.getFile 成功创建空文件,但 fileTransfer.download 失败并显示 FILE_NOT_FOUND_ERR,即使它使用相同的路径也是如此。

问题是我的文件是在 //sdcard/MyDir/test.pdf 中创建的(我确认使用 adb shell)但是 fileEntry 返回了一个路径没有 sdcard: //MyDir/test.pdf。 fileTransfer.download 使用此路径失败。它也因相对路径 MyDir/test.pdf 而失败。

如果我在其中使用“sdcard”对完整路径进行硬编码,我可以避免 FILE_NOT_FOUND_ERR(具体而言,在 FileTransfer.java 中,resourceApi.mapUriToFile 调用成功)但是随后我得到一个 CONNECTION_ERR 并且控制台显示“文件插件不能代表下载路径”。 (在 FileTransfer.java 中,filePlugin.getEntryForFile 调用返回 null。我假设它不喜欢路径中的“sdcard”。)

有没有更好的方法来指定fileTransfer.download中的目标路径?

var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf";  // using an absolute path also does not work

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
   fileSystem.root.getFile(relativeFilePath, { create: true }, function (fileEntry) {
      console.log(fileEntry.fullPath);  // outputs: "//MyDir/test.pdf"

      var fileTransfer = new FileTransfer();
      fileTransfer.download(
         downloadUrl,

         /********************************************************/
         /* THE PROBLEM IS HERE */
         /* These paths fail with FILE_NOT_FOUND_ERR */
         //fileEntry.fullPath,      // this path fails. it's "//MyDir/test.pdf"
         //relativeFilePath,        // this path fails. it's "MyDir/test.pdf"

         /* This path gets past the FILE_NOT_FOUND_ERR but generates a CONNECTION_ERR */
         "//sdcard/MyDir/test.pdf"
         /********************************************************/

         function (entry) {
            console.log("Success");
         },
         function (error) {
            console.log("Error during download. Code = " + error.code);
         }
      );
   });
});

如果有区别,我正在使用 Android SDK 模拟器。

最佳答案

我能够通过使用文件路径的 URI 来解决这个问题。 (根据@Regent 的评论,我还删除了对 fileSystem.root.getFile 的不必要调用。)

var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf";  // using an absolute path also does not work

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
   var fileTransfer = new FileTransfer();
   fileTransfer.download(
      downloadUrl,

      // The correct path!
      fileSystem.root.toURL() + '/' + relativeFilePath,

      function (entry) {
         console.log("Success");
      },
      function (error) {
         console.log("Error during download. Code = " + error.code);
      }
   );
});

关于android - PhoneGap FileTransfer.download 期望的路径与 FileSystem 提供的路径不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868738/

相关文章:

android - 使用 PubNub 为 Raspberry Pi 编写节拍器 Python 脚本

android - 创建带有选项列表的弹出窗口,例如 Youtube App

android - 覆盖android中的文件删除

android - 保持连接有效?

android - cordova 模拟 android 失败,出现 "device is still connecting"

javascript - Apache Cordova - session 销毁后我们如何保护页面

eclipse - 如何在 Eclipse 中设置 BlackBerry Phonegap 项目——MyApp.java 以加载 index.html?

android - PhoneGap 截图插件不适用于 Android?

javascript - 创建 Cordova 2.2.0 插件

objective-c - 如何在手机间隙使用原生xib?