Cordova iOS应用程序目录和文件url不同(UUID问题)

Cordova iOS应用程序目录和文件url不同(UUID问题)

问题描述:

我创建了一个从服务器获取图像并将它们保存到iPad的Cordova应用程序。但是,当试图在应用程序中显示图像时,图像将不会加载。这样的文件路径的一个示例可以是:Cordova iOS应用程序目录和文件url不同(UUID问题)

file:///var/mobile/Containers/data/Application/FC87E925-9753-4D9F-AE27-54FCF9B0451E/Documents/-media-3405-company.png 

然而,检查cordova.file.applicationDirectory变量时,我发现另一条路径,例如(注意,UUID不同,即使我检查在同一运行两个变量)

file:///var/containers/Bundle/Application/D8266D08-18A4-4293-B78A-B4597FC0C6B8/salesApp.app/ 

因此到documentation,正确的路径“应该”是:(然而,这并不工作)

file:///var/mobile/Applications/UUID/Documents/-media-3405-company.png 

这里是我使用加载图像,这是正确地保存到设备

const downloadFile = (url, fileName, callback) => { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (fs) => { 
      fs.root.getFile(fileName, { 
      create: true, 
      exclusive: false 
      }, (fileEntry) => { 
      const fileURL = fileEntry.toURL() 
      const fileTransfer = new FileTransfer() 
      fileTransfer.download(
       url, 
       fileURL, 
       (entry) => { 
       const file = entry.toURL() // <--- HERE 
       content.pushObject('Downloaded ' + entry + ' (' + fileName + ') ' + file) 
       callback(file) 
       }, 
       (error) => { 
       content.pushObject('error ' + error.code + '(' + fileName + ')') 
       if (error.code === FileTransferError.CONNECTION_ERR) { 
        downloadFile(url, fileName) // Try again 
       } else { 
        decrement(url) // Ignore this file 
       } 
       } 
      ) 
      }, (error) => { 
      alert(2) 
      }) 
     },() => { 
     alert(3) 
     }) 
    } 

更新的代码:为cordova.file.documentsDirectory检查的值,我发现第在它返回类似于:file:///var/mobile/Containers/Data/Application/{UUID}/Documents/的路径。

更新:下面的代码将返回两个不同的UUID:

alert(cordova.file.applicationDirectory); // file:///var/containers/Bundle/Application/54E0F914-C45B-4B8F-9067-C13AF1967760/salesApp.app/ 
alert(cordova.file.documentsDirectory); // file:///var/mobile/Containers/Data/Application/73806E90-90B4-488C-A68A-2715C3627489/Documents/ 

当检查路径entry.toURL()我得到相同的UUID作为一个在cordova.file.documentsDirectory返回。

+1

@马丁我猜你得使用entry.nativeURL检查出这方面的工作样本,这应有助于 - https://开头github上。com/gandhirajan/Cordova_File_Operations – Gandhi

+0

我想你必须使用entry.nativeURL检查这个应该帮助的工作示例 - github.com/gandhirajan/Cordova_File_Operations – Gandhi

+0

也在你提到的链接中 - https://cordova.apache.org/docs/ en/latest/reference/cordova-plugin-file /#ios-file-system-layout清楚地表明'applicationStorageDirectory'是只读的。你不能写信给它 – Gandhi

当你声称“图像不会加载”,那么你应该提供用于加载图像的代码。 你提供的代码是下载图像,它工作正常。

由于您没有提供用于加载我已经试过两件事情和他们两人的工作

  1. 公开赛InAppBrowser该文件中的图像的代码。我安装了cordova-plugin-inappbrowser并打开文件,如下所示window.open(file,'_blank');
  2. 将文件显示在img标签上。我在的index.html <img id="downloaded" src=""/>创建了一个img标签和我的回调我获得分配到src document.getElementById("downloaded").src = file;

他们两人的工作文件。

因此,您应该提供用于加载图像的代码,因为问题可能在那里。

您正在下载的路径正常。

由于文档已过时,您将得到不同的UUID。在Xcode 6/iOS8之前,应用程序沙箱的Bundle容器和Data容器位于同一个文件夹内(文档提到的是一个通用的UUID),但是由于Xcode 6/iOS8的应用程序文件(Bundle容器)位于路径中,应用程序数据文件在另一个(数据容器)中。

但是,这不应该是一个问题给你。

Answer that talks about the file structure changes

+0

感谢您的回答。我没有包含任何有关加载图像的代码,因为它有点复杂。基本上,我存储'fileEntry.toURL()'中的url(字符串),并将其显示在一个图像标签中。 ''使用Ember。 – feupeu

+0

我正在使用这个代码,如第2点所述'downloadFile('http://k33.kn3.net/taringa/0/3/5/1/2/9/marceloceirano99/21E.png','- media-3405-company.png',函数(文件){document.getElementById(“downloaded”).src = file;});'当我在远程Safari浏览器上检查它时获得这个src' '。此时,您还应该尝试在safari上调试您的应用程序 – jcesarmobile

+0

请注意'-media-3405-company.png'正在重命名为'-a%CC%82%E2%82% AC%C5%92A%CC%82%E2%82%AC%E 2%80%B9media-3405 - 共mpany.pa%CC%82%E2%82%AC%C5%92A%CC %82%E2%82%AC%E2%80%B9ng',我在'fileEntry.toURL()'上得到这个URL。 – jcesarmobile