上传图像为base64格式和压缩图像,然后通过改造发送到服务器?

我已经制作了上传图片,我在互联网上的教程中进行了改造。 这是我的代码:

AcademicClient.class

@Multipart @POST("/") Call postImage(@Part MultipartBody.Part image, @Part("name")RequestBody name); 

MainFeed.class

 File file = new File(filePath); RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"),file); MultipartBody.Part body = MultipartBody.Part.createFormData("upload",file.getName(),reqFile); RequestBody name = RequestBody.create(MediaType.parse("text/plain"),"upload_test"); Log.d("xxxxxxx",body + " ---- "+ name); AcademicClient client = ServiceGenerator.createService(AcademicClient.class); Call call = client.postImage(body,name); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { } @Override public void onFailure(Call call, Throwable t) { } }); 

如何转换它Base64并在改造前发送到服务器之前先压缩图像?

试试以下代码:

首先定义ByteArrayOutputStreambyte[]对象:

 bytearrayoutputstream = new ByteArrayOutputStream(); byte[] BYTE; 

第二个定义未压缩的Bitmap (bitmap1),如下所示:

  bitmap1.compress(Bitmap.CompressFormat.JPEG,40,bytearrayoutputstream); BYTE = bytearrayoutputstream.toByteArray(); 

第三个将byte[]转换为Base64

  String base64 = Base64.encodeToString(BYTE, Base64.DEFAULT); Bitmap compressedBitmap = BitmapFactory.decodeByteArray(BYTE,0,BYTE.length); 

第四,最后你得到CompressedBase64转换后的图像:

现在,您可以直接发送Base64图像而无需使用MultiPart