[英]upload image in base64 format and compressed image before sending to server with retrofit?
我已經從Internet教程中獲得了經過改造的上傳圖像。 這是我的代碼:
AcademicClient.class
@Multipart
@POST("/")
Call<ResponseBody> 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<ResponseBody> call = client.postImage(body,name);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
如何將其轉換為Base64並先壓縮圖像,然后再將其發送到翻新的服務器?
試試下面的代碼:
首先定義ByteArrayOutputStream
和byte[]
對象:
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);
第四,最后得到Compressed
和Base64
轉換的圖像:
現在,您無需使用MultiPart
即可直接發送Base64
圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.