如何捕获和存储图像?

时间:2015-12-17 08:26:55

标签: angularjs image cordova ionic-framework

我想捕获图像并将它们发送到服务器。

所以,我使用了this tutorial

我的控制器:

$cordovaCamera.getPicture(options).then(function(imageData) {

        // 4
        onImageSuccess(imageData);

        function onImageSuccess(fileURI) {
            createFileEntry(fileURI);
        }

        function createFileEntry(fileURI) {
            window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
        }

        // 5
        function copyFile(fileEntry) {
            var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
            var newName = makeid() + name;

            window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
                fileEntry.copyTo(
                    fileSystem2,
                    newName,
                    onCopySuccess,
                    fail
                );
            },
            fail);
        }

我收到此错误:

  

!JavaScript错误:'未定义'不是一个功能(评估   ' window.resolveLocalFileSystemURL(fileURI,copyFile,fail)')

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你需要失败功能。

function fail(error) {
            console.log("fail: " + error.code);
        }

答案 1 :(得分:0)

首先尝试使用图像库(Camera.PictureSourceType.PHOTOLIBRARY),当它工作时,更改为CAMERA。在" getPicture"图像在缓存文件夹中(在我的情况下" pathAppCache"。它需要将它们复制到您的文件夹中(在我的情况下" pathAppImagens"。

尝试

        $scope.choosePhoto = function () {


        var options = {
            quality: 50,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
            allowEdit: false,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 200,
            targetHeight: 200,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false,
            correctOrientation: true
        };            

        $cordovaCamera.getPicture(options).then(function (imagem) {            
            console.log("getPicture" + imagem);

            // Copy file to my imagem folder  
            $cordovaFile.copyFile(pathAppCache, "modified.jpg", pathAppImagens, "logo_empresa.jpg").then(function (success) {

                console.log("copyFile " + success);
            }, function (error) {
                console.log("copyFile " + error);
            });
        }, function (error) {
            console.log("getPicture " + error);
        });

    };

首先我们设置目录

        if (ionic.Platform.isAndroid()) {
            pathApp = cordova.file.externalDataDirectory;
            pathAppCache = cordova.file.externalCacheDirectory;
            pathAppImagens = pathApp + "imagens/";
        };                
        // Se estiver rodando na plataforma iOS
        if (ionic.Platform.isIOS()) {
            pathApp = cordova.file.documentsDirectory;
            pathAppCache = cordova.file.cacheDirectory;
            pathAppImagens = pathApp + "imagens/";
        };

详情请见http://sourcefreeze.com/cordova-camera-plugin-example-using-ionic-framework/