c.k
c.k

Reputation: 1105

Convert URI image to base64 format using cordova-plugin-camera

Using this function I am able to get the uri of the image from my phone gallery.

function getPhoto() {
      navigator.camera.getPicture(onPhotoSuccess, onFail, 
        {quality: 50,
         sourceType: pictureSource.SAVEDPHOTOALBUM,
         destinationType: destinationType.FILE_URI,
        });
    }

function onPhotoSuccess(imageUri) {
        var smallImage = document.getElementById('smallImage');
        smallImage.style.display = 'block';
        smallImage.src = imageUri;

        console.log(imageUri);
    }

The console output is showing the uri like this: content://com.android.providers.media.documents/document/image%3A76755

Is it possible to convert the imageUri to base64 format and not using the <canvas>? I am using Andriod phone and this is a cordova app.

Upvotes: 1

Views: 1258

Answers (2)

Sarantis Tofas
Sarantis Tofas

Reputation: 5167

Yes, just change FILE_URI to DATA_URL This will give you the image in a base64 format

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

Use destinationType: Camera.DestinationType.DATA_URL

From the docs:

Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible

Upvotes: 2

Related Questions