我使用以下方法编码图像
Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] byteArrayImage = baos.toByteArray();
encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);`
我发送的数据来自改造,如下所示:
newFoodModel = new NewFoodModel(food_name,food_unit,food_price,encodedImage,providerId,foodCategoryId);
Call<ServiceResponse>serviceResponseCall = uploadFoodApi.getResponseResult(newFoodModel);
serviceResponseCall.enqueue(new Callback<ServiceResponse>() {
@Override
public void onResponse(Call<ServiceResponse> call, Response<ServiceResponse> response) {
if(response.code() == 200){
ServiceResponse serviceResponse = response.body();
Toast.makeText(AddFoodActivity.this, serviceResponse.getMessage(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AddFoodActivity.this, response.code()+","+response.message(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ServiceResponse> call, Throwable t) {
Toast.makeText(AddFoodActivity.this, "Check connection", Toast.LENGTH_SHORT).show();
}
});
我不知道我在哪里做错了,来自服务器的响应是错误代码:500,内部服务器错误。
我的回复模型类:
public class ServiceResponse {
@SerializedName("ResultState")
@Expose
private Boolean resultState;
@SerializedName("Message")
@Expose
private String message;
public Boolean getResultState() {
return resultState;
}
public void setResultState(Boolean resultState) {
this.resultState = resultState;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
处理请求的接口:
public interface UploadFoodApi {
@POST("after_part_of_base_url")
Response<ServiceResponse> getResponseResult(@Body NewFoodModel newFoodModel);
}
答案 0 :(得分:0)
由于图片尺寸,图片未上传:
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream );// image quality was kept 100%
我通过降低质量来解决问题,
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream ); //it is now scaled down to 80% of original image.