【问题标题】:PhoneGap - Error code 5 when trying copy file to dataDirectory, but why?PhoneGap - 尝试将文件复制到 dataDirectory 时出现错误代码 5,但为什么?
【发布时间】:2017-09-27 02:37:41
【问题描述】:

在我的应用程序中,用户可以从图库中选择一张图片,该图片将用于“用户详细信息”。因此,创建了一个屏幕,他存在一个名为“file”的输入文件。我需要将选择的图像复制到 externalDataDirectory。

我的问题是如何从选择复制的文件中获取 URI?

我的代码是……但每次都得到错误代码9,但我不知道这是什么意思。

inputFile = $( '.file' ).val();

window.resolveLocalFileSystemURL( cordova.file.externalDataDirectory, function( myFileEntry ){


    window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, function(fileEntry){

        parentEntry = new DirectoryEntry( inputFile );


        myFileEntry.getDirectory( "TRETA", {create: true, exclusive: false}, function(dir){ 

            // copy the file to a new directory and rename it
            fileEntry.root.copyTo( dir, "arquivo", function(entry){
                console.log("New Path: " + entry.fullPath);
            }, function(error){
                console.log(error);
            });

        }, fail); 


    }, function(error){
        console.log( error );
    });

}, fail);

fffff

【问题讨论】:

    标签: android cordova phonegap html5-filesystem


    【解决方案1】:

    (注意:假设您要复制位于 app-root 文件夹中的文件。我们将其命名为 appURI)。

    我只能为自己说话,但LocalFileSystem.PERSISTENT 用我的安卓手机(测试环境)指向file:///storage/emulated/0/。使用此基本 URI,默认情况下您没有读/写权限,但在其子文件夹中。

    您要访问的文件夹应如下所示:file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/ (appURI)

    在这个文件夹中你有读/写权限。因此,您可以使用自己的逻辑自行获取此网址,也可以在我的安卓手机上使用指向file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/cache/LocalFileSystem.TEMPORARY,所以我想它应该与您相同。

    但都在一起:

    function getAppURI(callback) {
      window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (filesystem) {
        var cacheDir = filesystem.root.toURL();
        var startPointCacheFolderName = cacheDir.match(/\/\w+\/$/) [0];
        callback(cacheDir.replace(startPointCacheFolderName, '') + '/');
      }, function (error) {
        console.log('no access to app-filesystem');
      }
      );
    }
    

    最后我们可以使用正确的appURI

    getAppURI(function (appURI) {
      window.resolveLocalFileSystemURL(appURI, function (fileSystem) {
        fileSystem.getFile('fileToCopy.txt', {
          create: false, // try true first to make sure that there are no other problems around
          exclusive: false
        }, function (fileEntry) {
          window.resolveLocalFileSystemURL(appURI+"NAME_OF_A_SUBFOLDER_YOU_WANT_TO_COPY_TO OTHERWISE_REPLACE_THIS_STRING", function (newFileEntry) {
            fileEntry.copyTo(newFileEntry, 'CopiedFile.txt', function (result) {
              console.log("save successfully:", result);
            }, function (err) {
              console.log("err-fileEntry.copyTo: ",err);
            });
          }, function (err) {
            console.log("err-window.resolveLocalFileSystemURL: ",err);
          });
        }, function (err) {
          console.log("err-fileSystem.getFile: ",err);
        });
      }, function (err) {
        console.log("err-resolveLocalFileSystemURL: ",err);
      });
    });
    

    希望这会有所帮助。


    还值得一提的是,错误代码并不总是表明操作无法成功的真正原因。对于这个question,还有一个读/写权限错误,并且错误代码(ENCODING_ERR 5)是相同的(看看cmets)。

    【讨论】:

    • 您的解释很有帮助,但我再次无法从输入中获取文件...在日志中我得到了这个... appURI = file:///storage/emulated/0/Android/ data/br.com.johnhenrique.myapp/ 是在输入文件中选择的文件的正确 URI:C:\fakepath\IMG-20170926-WA0065.jpg 我只是使用 Android 手机(Android 6.1),但每次都得到“C:\fakepath ……”。如果我理解正确,我需要将 'fileToCopy.txt' 替换为 "$( '.file' ).val()",对吗?
    • 谢谢。是的,确切地说,如果我正确理解您的问题,fileToCopy 应该替换为 $('.file').val() 以及 CopiedFile 替换为“arquivo”。
    • 我有其他问题,无法正常工作,每次出现错误“err-fileSystem.getFile: {code:1}”,我只是按照之前所说的替换...
    • fileToCopy 或此文件的路径 ($('.file').val()) 必须存储在您的 android 设备上,否则它将无法工作。错误代码 1 表示 NOT_FOUND_ERR,因此您的设备上不存在此文件。
    • 是的,你是对的。但是该文件位于Android的本地存储中(存在于设备中),我只是添加了console.log来查看收到了什么信息,我不知道为什么每次文件URI都显示为“C:\ fakepath”(我' m 使用 Google Chrome 检查)。
    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2017-11-05
    • 2020-01-13
    相关资源
    最近更新 更多