将图像内容从相机加载到文件

时间:2013-04-30 11:10:25

标签: cordova angularjs parse-platform ripple

我正在使用以下drictive使用phonegap API拍照(或从库中选择):

MyApp.directive('Camera', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            elm.bind('click', function() {
                navigator.camera.getPicture(function (imageURI)
                {
                    scope.$apply(function() {
                        ctrl.$setViewValue(imageURI);
                    });
                }, function (err) {
                    ctrl.$setValidity('error', false);
                },
                //Options => http://docs.phonegap.com/en/2.6.0/cordova_camera_camera.md.html#Camera
                { quality: 50,
                  destinationType:Camera.DestinationType.FILE_URI                      
                })
            });
        }
    };
});

使用chrome上的ripple模拟器返回一个看起来像的URI,我可以看到粘贴这个URI。

blob:http%3A//localhost%3A8080/8e18de30-d049-4ce2-ae88-8500b444581e

我的问题是加载此URI

$scope.updateUserProfile = function (user) {

       var myPicfile = $http.get(user.myPicture);

       dataService.uploadPicture . . . some code to update the picture to Parse

    }

*注意:I cannot use phonegap filetransfer together with parse.com :

当我这样做时,我得到:

enter image description here

我提出的要求是:

uploadPicture:function uploadPicture(user,callback)             {                 var serverUrl ='https://api.parse.com/1/files/'+ user.Nick;

            $http({
                method: 'POST',
                url: serverUrl,
                data: user.myPicture,
                headers: {'X-Parse-Application-Id': PARSE_APP_ID,
                    'X-Parse-REST-API-Key': PARSE_REST_API_KEY,
                    'Content-Type': 'text/plain'
                }
            })

关于如何将图像内容添加到文件然后我可以愉快地上传到Parse.com的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:4)

我终于解决了这个问题,因为我的最终目标是将其与phonegap with the info in this post一起使用。 。非常感谢Raymond Camden!

function gotPic(data) {

window.resolveLocalFileSystemURI(data, function(entry) {

var reader = new FileReader();

reader.onloadend = function(evt) {
    var byteArray = new Uint8Array(evt.target.result);
    var output = new Array( byteArray.length );
    var i = 0;
    var n = output.length;
    while( i < n ) {
        output[i] = byteArray[i];
        i++;
    }                
    var parseFile = new Parse.File("mypic.jpg", output);

    parseFile.save().then(function(ob) {
            navigator.notification.alert("Got it!", null);
            console.log(JSON.stringify(ob));
        }, function(error) {
            console.log("Error");
            console.log(error);
        });

}

reader.onerror = function(evt) {
      console.log('read error');
      console.log(JSON.stringify(evt));
  }

entry.file(function(s) {
    reader.readAsArrayBuffer(s);
}, function(e) {
    console.log('ee');
});

});
}
相关问题