Zakynthos
Zakynthos

Reputation: 101

Is it a good practice to send images in base64 string from android to PHP?

In my android application I Capture image or select image from gallery. then I encode that image in base64 string. then I send that image to PHP in JSON format or with normal post request. then I decode base64 string to PNG image and save on server. then save path of that image to mySQL image_url column.

This is the way I am currently working now..Which create image file.. but sometime it creates corrupt image file(full black or 0 KB or 20 KB in size). and Picasso does not work on such files. It shows decoder failed error of picasso.

This way reduces Image quality 1.5 MB image(on device) to 50 KB image (on server). my code for encoding to base64 is,

`   // method for bitmap to base64
    public static String encodeTobase64(Bitmap image) {
        Bitmap immage = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
        return imageEncoded;
    }`

If anyone have suggestions please help. or other way for sending and saving images.

EDITS are welcomed.

Upvotes: 0

Views: 3007

Answers (1)

Puneet Arora
Puneet Arora

Reputation: 199

It's a good practice "If" you are saving your images as strings in your database, but since you are converting the image back to png and saving just the path in mysql (which is a very good practice), doesn't make sense to convert to base64.

I have built a web application as well as mobile apps which display/ store multiple images. I would suggest, don't encode it, just send images in chunks and your php code will combine those chunks, create an image file and store the file path of the image in the database(image_url).

Upvotes: 1

Related Questions