Send camera image to server via Retrofit

7

I learned to send an image of the drawable folder to my server on the network using lib Retrofit, but I am not able to send a ImageView the camera. I create a class that converts the drawable image to bytes , which it will get from MainActivity by accessing the drawable folder. How to do this by accessing a ImageView . I can access bitmap of Imageview , but I'm not sure how to send bitmap to the conversion class to byte ; in fact, not how to make the conversion class receive this bitmap . I'll leave the conversion class and MainActivity codes. The question is: how to treat this code to send a bitmap accessed in a ImageView : Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

//Clsse para conversão do bitmap para byte
 public class BinaryBytes {

    public static byte[] getResourceInBytes( Context context, int resource )   {
    final Bitmap img = BitmapFactory.decodeResource(context.getResources(), resource);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    img.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    return( byteArray );
}

public static String getResourceName( Context context, int resource ){
    return( context.getResources().getResourceEntryName(resource) );
}

}



// Código do acesso à pasta drawable e do envio para o servidor

        String imageName = BinaryBytes.getResourceName(this, R.drawable.jeep); //BinaryBytes
        byte[] imageBinary = BinaryBytes.getResourceInBytes(this, R.drawable.jeep);

        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), imageBinary);

        call = carAPI.sendImg("send-img", imageName, requestBody);
        call.enqueue(new Callback<Car>() {
            @Override
            public void onResponse(Response<Car> response, Retrofit retrofit) {
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i(TAG, "Error SEND IMG: " + t.getMessage());
            }
        });
    
asked by anonymous 10.11.2015 / 12:42

2 answers

7

With Retrofit you can use Multipart Upload to send an image. But you need to save the photo to your device, even if it's just to upload and then delete it. In Documentation teaches you how to send a Bitmap object as follows:

    1 - Generate a Bitmap object from the photo
    2 - Save this Bitmap to a JPG file
    3 - Create a TypedFile from the JPG file:
TypedFile image = new TypedFile("image/jpeg", imageFileName);
    4 - Create an interface to use with Retrofit and add Annotation @Multipart in the upload method. Notice that TypedFile is the type of file that will be sent, Retrofit is responsible for covering it to bytes [] and executing the POST request:
    @Multipart
    @POST("/user/me/avatar?access_token={access_token}")
    void uploadAvatar(@Name("access_token") String accessToken, @Name("avatar") TypedFile image, retrofit.http.Callback<Boolean> callback);
    
12.11.2015 / 14:35
1

To send image to the server I used it in the new version:

@Multipart
@POST("xpto/upload")
public Call<AnuncioRest> upload(@Part("file\"; filename=\"image.jpg\"") RequestBody file,
                                @Part("descricao") RequestBody descricao);

And I call it like this:

 public AnuncioRest createAnuncio(final Anuncio anuncio, final AnuncioActivity anuncioActivity){
    try {
        showProgress(anuncioActivity,"Aguarde","Criando seu anúncio...");
        AnuncioService service = ServiceGenerator.createService(AnuncioService.class);

        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), anuncio.getFotoProduto());
        RequestBody descricao= RequestBody.create(MediaType.parse("text/plain"), anuncio.getDescricao());
        Call<AnuncioRest> call = service.upload(requestBody, descricao);
        call.enqueue(new Callback<AnuncioRest>() {
            @Override
            public void onResponse(Call<AnuncioRest> call, Response<AnuncioRest> response) {

            }
        }...
    
16.06.2016 / 23:45