在Android中使用的Base64改造图片上传 [英] Retrofit image upload using Base64 in android

查看:1064
本文介绍了在Android中使用的Base64改造图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以Base64格式使用改装上传的图片。

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

要位图转换为Base64,

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;
        }
    }

我不希望使用Typedfile上传的图片。

I do not want to upload the image using Typedfile.

我的要求方法如下,

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

Base64的转换是正确的,但我没有得到服务器上的图像。什么我在上面做错了?

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

推荐答案

发表您的Base64编码字符串作为您的要求进行现场参数。还可以使用FormUrlEn codeD标注,增加了应用程序/ x-WWW的形式urlen codeD的内容类型。

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);

这篇关于在Android中使用的Base64改造图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆