2

I am selecting a image from my gallery on an Android device. The image is returned as a file-uri and I use the filereader to convert the file-uri to a base64 string which I can use to send to the server and also display the image in my app. Problem is that when I try to display the image, I only get a blank picture. The same thing if I try to decode the base64 string, the image is not recognized as an image at all. Function that encodes the file-uri:

    encodeImageToBase64: function(imageURI){
        var fileReader = new FileReader();
        var blob = new Blob([imageURI], { type: 'image/jpeg'});
        var result = {};

        fileReader.onloadend = function(){
            result = fileReaders.result.split(',')[1];
            console.log("Result: " + result);
        }
        fileReader.readAsDataURL(blob);
    }

The string I get as a result is as short as ZmlsZTovLy9kYXRhL2RhdGEvbm8uY2Fyd2ViLm1vYmlsZS9jYWNoZS90bXBfSU1HXzIwMTUxMTI3XzA0MjEyNjE2MDg5NDU4ODYuanBn. When I take a picture with my app I get a dataURL which is much larger, so I am wondering if I am doing this encoding the wrong way or am I missing something here? Any help to point me in the right direction is much appreciated :)

user2236165
  • 741
  • 2
  • 11
  • 24
  • Have tried this one http://stackoverflow.com/questions/4478863/show-image-from-blob-in-javascript – Jitendra Khatri Dec 22 '15 at 11:19
  • But this doesnt create a base64 string? I still need the output to be a base64 string when I upload the images to the server? – user2236165 Dec 22 '15 at 11:27
  • For showing the image you can use blob data and for uploading that image you can create base64 for the same. – Jitendra Khatri Dec 22 '15 at 11:45
  • But do we have any idea why the base64 encoded string I get from my function do not decode to the original image? I still feel that is the issue. – user2236165 Dec 22 '15 at 12:04
  • Your result is just the base64-encoded version of the URI: `file:///data/data/no.carweb.mobile/cache/tmp_IMG_20151127_0421261608945886.jpg` – TripeHound Dec 22 '15 at 12:04

0 Answers0