如何显示在Angularjs从PhoneGap的/科尔多瓦相机API移动图片(How to disp

2019-10-21 03:32发布

继我以前的帖子( 科尔多瓦照相机API的使用 ),我找到了一种方法来移动捕获使用下面的代码,现在用科尔多瓦3.5.0本地app文件夹的图片:

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存储似乎并不一致发生。 我想不出什么不顺心,为什么有时(很少)的作品。 任何想法将是非常赞赏。

文章来源: How to display moved pictures from Phonegap/Cordova Camera API under Angularjs