Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit image upload using Base64 in android

I am trying to upload the image using retrofit in Base64 format.

To convert bitmap to Base64,

public static String convertImageToStringForServer(Bitmap imageBitmap){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if(imageBitmap != null) {
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
            byte[] byteArray = stream.toByteArray();
            return Base64.encodeToString(byteArray, Base64.DEFAULT);
        }else{
            return null;
        }
    }

I do not want to upload the image using Typedfile.

My request method is below,

        @Multipart
        @POST("/pingpong")
        void doPingpong(@Part ("access_token") TypedString accessToken,
        @Part("image") TypedString profileImage,
        @Part("pixels") TypedString doPingPong,Callback<pixelsPing> callback);

Base64 conversion is correct, but I am not getting the image on server. What am i doing wrong in the above?

like image 813
Custadian Avatar asked Jun 08 '15 04:06

Custadian


1 Answers

Post your Base64 string as a Field parameter of your request. Also use the FormUrlEncoded annotation, which adds "application/x-www-form-urlencoded" content type.

@POST("/upload")
@FormUrlEncoded
void upload(@Field("image") String base64, Callback<ResponseObject> callback);
like image 117
shmoula Avatar answered Oct 11 '22 19:10

shmoula