Skip to content Skip to sidebar Skip to footer

Cordova-android-camera: How To Save Base64 Encoded Jpg To Android File System

I am succesfully creating directories, if necessary, where I want to in the Android file system on my device, a Moto X 1st gen. I am successfully saving a plain text file, but can

Solution 1:

I replaced line 17 with

writer.write(b64toBlob(img.src.replace(/data:image\/jpeg;base64,/, ''), 'image/jpg', 512));

Here is my b64toBlob function

functionb64toBlob(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);
        var byteNumbers = newArray(slice.length);
        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        var byteArray = newUint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    var blob = newBlob(byteArrays, {type: contentType});
    return blob;
}

This works well with saving jpg images to the local Android file system.

Post a Comment for "Cordova-android-camera: How To Save Base64 Encoded Jpg To Android File System"