如何在Angularjs下显示来自Phonegap / Cordova Camera API的移动图片

时间:2014-10-14 22:58:29

标签: angularjs cordova steroids

按照我之前的帖子(usage of Cordova camera API),我找到了一种方法,使用以下代码将捕获的图像移动到本地app文件夹,现在使用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中的文件名似乎不会一致地发生。我无法弄清楚出了什么问题以及为什么它有时(非常罕见)有效。 任何想法都会非常感激。

0 个答案:

没有答案