在Android中使用Base64改造图片上传

Cus*_*ian 6 base64 android gson retrofit okhttp

我正在尝试使用Base64格式的改造来上传图像。

要将位图转换为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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不想使用Typedfile上传图像。

我的请求方法如下

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

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

shm*_*ula 5

将您的Base64字符串作为请求的Field参数发布。还可以使用FormUrlEncoded批注,该批注添加“ application / x-www-form-urlencoded”内容类型。

@POST("/upload")
@FormUrlEncoded
void upload(@Field("image") String base64, Callback<ResponseObject> callback);
Run Code Online (Sandbox Code Playgroud)