【问题标题】:Moving a photo to local data directory in Onsen/Cordova app在 Onsen/Cordova 应用程序中将照片移动到本地数据目录
【发布时间】:2016-06-05 15:49:00
【问题描述】:

我正在开发一个 Onsen/Cordova 移动应用程序,使用 Monaca 进行开发,并使用 Android 手机进行测试,该应用程序可以将照片附加到应用程序的输出中。我有这段代码可以拍照并将其复制到cordova.file.dataDirectory:

navigator.camera.getPicture(
    function(imgUri) { // Success callback
        // Get FileEntry object for the new photo
        window.resolveLocalFileSystemURL(
        imgUri,
        function(fileEntry){
            alert('Success - Before: ' + JSON.stringify(fileEntry));
            newFileName = fileEntry.name;
            window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
                function(dirEntry) {
                    // move the file to a new directory and rename it
                    fileEntry.moveTo(dirEntry, newFileName, function success() {
                        alert('Copy operation successful - After moving: ' + JSON.stringify(fileEntry) 
                            + '\nURL: ' + fileEntry.toURL());
                        $scope.photos.unshift(fileEntry.toURL());
                        $scope.$apply();
                    }, 
                    function failure(){
                        alert('Failed to move file.');
                    });
                },
                function failure(){
                    alert('Failed to get local FS for data directory.');
                });
        },
        function failure(){
            alert('Failed to get local FS for new image.');
        });
    },
    function() { // Error callback
        // We end up here if the photo selection/shoot is cancelled. So we do nothing.
        return;
    },
    opts // Options 
);

不幸的是,尽管触发了所有成功回调,但警报显示照片在移动前后的相同路径 - file://storage/emulated/0/Android/data/ mobi.monaca.debugger/cache/ - 而 cordova.file.dataDirectory 的路径是 file:///data/user/0/mobi.monaca.debugger.files/ 。

这段代码有什么不正确的地方?

【问题讨论】:

    标签: cordova cordova-plugins onsen-ui monaca


    【解决方案1】:

    您是否也尝试过“copyTo”而不是“moveTo”?我已经阅读了我的旧代码,并且我已经看到我使用了 copyTo。我记得 moveTo 被操作系统阻止(在某处谷歌上阅读),但我真的不知道哪个 Android 版本。 如果“copyTo”功能适合您,这可能就是原因。

    编辑:这是我的工作代码。检查并查看差异(可能是文件系统?)

    function manipulate_img_url(fileUri) {
        console.log("fileUri: "+fileUri);
        window.requestFileSystem(PERSISTENT, 0, function(fileSystem) {
            window.resolveLocalFileSystemURL(
              fileUri,
              function(fileEntry){
                  console.log("Start elaboration: "+JSON.stringify(fileEntry)); // This give me the native url: 'nativeURL':"file:///storage/emulated/0/Android/data/com.intel.appx.RotarApp.xwalk15/cache/1465151572273.jpg"
                    newFileUri  = cordova.file.dataDirectory + "images/";
                  console.log("newFileUri: "+newFileUri);
                    oldFileUri  = fileUri;
                  console.log("oldFileUri: "+oldFileUri);
                    fileExt     = oldFileUri.split('.').pop();
                  console.log("fileExt: "+fileExt);
    
                    newFileName = "my_avatar" + "." + fileExt;
                  console.log("newFileName: "+newFileName);
                    window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
                            function(dirEntry) {
                                // move the file to a new directory and rename it
                                fileEntry.copyTo(dirEntry, newFileName, successCallback, errorCallback);
                            },
                            errorCallback);
              },
              errorCallback);
        }, function(e) {
          console.error('Error requesting file system: '+ e.code);
        });
    }
    function successCallback(entry) {
        console.log("entry: "+JSON.stringify(entry));
        console.log("New Path: " + entry.fullPath);
        console.log("native url: "+entry.nativeURL); // <<--- this is the new url: file:///data/data/com.intel.appx.RotarApp.xwalk15/files/my_avatar.jpg
    }
    

    【讨论】:

    • 不幸的是,结果似乎是一样的。仅触发成功回调,但路径在 copyTo 前后保持不变。而且和cordova.file.dataDirectory不一样。
    猜你喜欢
    • 1970-01-01
    • 2015-03-02
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多