-1

I am encoding image using following method`

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

I am sending the data from using retrofit like following :

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

I don't know where i am doing wrong , the response from server is error code:500 , internal server error.

my response model class :

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

the interface which is handing the request :

public interface UploadFoodApi {
    @POST("after_part_of_base_url")
    Response<ServiceResponse> getResponseResult(@Body NewFoodModel newFoodModel);
}
hasan
  • 57
  • 1
  • 13

1 Answers1

0

Image was not uploading because of image size , before it was :

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream );// image quality was kept 100%

I solved the problem by scaling down the quality,

bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream ); //it is now scaled down to 80% of original image.
hasan
  • 57
  • 1
  • 13