1

在我之前的帖子(Cordova 相机 API 的使用)之后,我找到了一种使用以下代码将捕获的图像移动到本地应用程序文件夹的方法,现在使用 Cordova 3.5.0:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {    

    function onCameraFail(message) {
        console.log('Failed because: ' + message);
        $scope.$apply();
    }

    function fileError(message) {
        console.log('Failed because: ' + message);
        $scope.$apply();
    }

    $scope.takePhotoFromCamera = function() {
        navigator.camera.getPicture(onCameraSuccess, onCameraFail, { 
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: 1,      // 0:Photo Library, 1:Camera, 2:Photo Album
            encodingType: 1,     // 0:JPG, 1:PNG
            allowEdit: true,
            correctOrientation: true,
            saveToPhotoAlbum: false,
            targetWidth: 600
        });
        $scope.$apply();
    };   

    function onCameraSuccess(imageURI) {
        window.resolveLocalFileSystemURL(imageURI, gotFileObject, fileError);
    }    

    //-- Move photo file to permanent location (localhost/CORS incompatibility) --
    function fileMoved(file) {
        $scope.$apply(function () {
            $scope.favourite.photo.push("/" + file.name);
        });        
    }

    function gotFileObject(file) {
        steroids.on('ready', function() {
            var targetDirURI = "file://" + steroids.app.absoluteUserFilesPath;
            var fileName = "pic" + $scope.id + "-" + $scope.favourite.photo.length + ".png";
            window.resolveLocalFileSystemURL(targetDirURI, function(directory) {
                file.moveTo(directory, fileName, fileMoved, fileError);
            }, fileError);
            if ($scope.favourite.photo.length == 0) {
                window.location.reload();
            }
        });
    }
}

此代码似乎非常随机地工作。图像文件似乎总是正确地移动到本地应用程序根文件夹,但 fileMoved 函数内 $scope.favourite.photo 中存储的文件名似乎并不一致。我无法弄清楚出了什么问题以及为什么有时(很少)会起作用。任何想法将不胜感激。

4

0 回答 0