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

我创建了一个Cordova应用程序,从服务器获取图像并将其保存到iPad.但是,尝试在应用程序中显示图像时,图像将无法加载.这种文件路径的一个例子可能是:

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,(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()的路径时,我获得与cordova.file.documentsDirectory中返回的UUID相同的UUID.

解决方法

当您声称“图像无法加载”时,您应该提供用于加载图像的代码.
您提供的代码是下载图像,它工作正常.

由于你没有提供用于加载图像的代码,我尝试了两件事,而且两者都有效

>在InAppBrowser上打开文件.我安装了cordova-plugin-inappbrowser并打开了这个window.open文件(文件,’_ blank’);
>在img标签上显示文件.我在index.html中创建了一个img标签< img id =“已下载”src =“”/>在我的回调中,我将获得的文件分配给src document.getElementById(“已下载”).src = file;

他们俩都工作了.

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

您下载的路径是可以的.

您获得了不同的UUID,因为文档已过时.在Xcode 6 / iOS8之前,应用程序沙箱具有Bundle容器和同一文件夹中的Data容器(文档提到的具有公共UUID的文件夹),但是从Xcode 6 / iOS8开始,app文件(Bundle容器)位于路径中App数据文件位于另一个(数据容器)中.

但这对你来说应该不是问题.

Answer that talks about the file structure changes

相关文章

公司前端界面用的是vue,我要嵌入到Android中生成App第一步:...
Q:我用cordova开发项目,想在app内跳转外部链接,安装了cord...
我正在使用https://github.com/arnesson/cordova-plugin-fir...
一、Cordova的基础点在混合式应用中,我们通过现有的Cordova...
cordova自定义插件注意:存放自定义cordova插件目录不能有空...
一、问题VueAPP中有一个文件下载功能,用了各种方法来实现下...