Alex Rumba Nicked Alex Rumba Nicked - 1 year ago 72
Android Question

Broken image being displayed when using the image path returned from cordovacapture captureimage

I have tried various solutions: $cordovaCapture, $cordovaCamera(DATA_URL can display the picture, but i want file_URI to do the same).

here is my code snippet:

$scope.addImage = function() {
var options = {limit: 1};
$cordovaCapture.captureImage(options).then(function(imageData) {
// var jsonobj=angular.toJson(imageData);
$scope.profile.image = imageData[0];
console.log($scope.profile.image.localURL);//the path to upload
document.getElementById('myImage').src = "'"+$scope.profile.image.localURL+"'";//have already tried without the quottes
/* window.plugins.Base64.encodeFile($scope.profile.image.localURL,function(base64){ // Encode URI to Base64 needed for contacts plugin
$scope.profile.image.preview = base64;
// Success! Image data is here
}, function(err) {

i even tried whitelisting certainties in the module as in :

.config( [
function( $compileProvider )

It didn't help either. I am testing the project in both device and emulator. I even tried base64 encoding the file from the path. Nothing works as to display the recently taken picture. The path that i retrieve is like this:

Answer Source

Instead of using file_URI to upload the image. I used data_URL,converted the image to blob and used the cordova-file-transfer plugin to upload the file to the server. In that way, i could use the base64 encoded image on the html side as well as upload at the same time.

 $scope.captureImage = function() {
  , cameraError, {
                destinationType: Camera.DestinationType.DATA_URL,
                correctOrientation: true

        var cameraSuccess = function(imageData) {
            $scope.profileImageSource = imageData;
            $scope.changeImage = function(base64Data, contentType) {
                contentType = contentType || '';
                var sliceSize = 512;
                var byteCharacters = atob(base64Data);
                var bytesLength = byteCharacters.length;
                var slicesCount = Math.ceil(bytesLength / sliceSize);
                var byteArrays = new Array(slicesCount);

                for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
                    var begin = sliceIndex * sliceSize;
                    var end = Math.min(begin + sliceSize, bytesLength);

                    var bytes = new Array(end - begin);
                    for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
                        bytes[i] = byteCharacters[offset].charCodeAt(0);
                    byteArrays[sliceIndex] = new Uint8Array(bytes);
                return new Blob(byteArrays, {
                    type: contentType

            $scope.picture = $scope.changeImage(imageData, 'image/png');


<img ng-src="data:image/gif;base64,{{profileImageSource}}">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download