Send camera image to server by Retrofit

Asked

Viewed 1,075 times

7

I learned how to send a picture of the folder drawable to my network server using lib Retrofit, but I’m not getting it from a ImageView, received from camera capture. I create a class that converts the image from the drawable for bytes, who will receive from MainActivity, accessing the folder drawable. How to do this by accessing a ImageView. I know how to access the bitmap of Imageview, but I don’t know how to send the bitmap for the conversion class to byte; in fact, nor how to make the conversion class receive this bitmap. I’ll leave the codes for the conversion class and the MainActivity. 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());
            }
        });
  • 1

    http://stackoverflow.com/questions/27209319/sending-an-image-file-post-with-retrofit

  • 1

    Caique, the link doesn’t address my problem.

  • 1

    Try for the path photo to send by Retrofit as a TypedFile

  • 1

    The photo is temporarily in an Imageview; because it was captured by the smartphone camera. I can access the Imageview bitmap, but I’m not able to send it to the Binarybytes class because it expects an image and a name of the image that is in the project. As my image is not in the project but in an Imageview, I cannot make the necessary changes.

2 answers

7

With Retrofit you can use Multipart Upload to send an image. But it is necessary that you save the photo on your device, even if only to upload and then delete it. In the documentation teaches how to send a Bitmap object as follows:

    1 - Generates a Bitmap object from the photo
    2 - Save this Bitmap in 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 to the upload method. Note that Typedfile is the file type that will be sent, Retrofit takes care of coverting 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);

1

To send image to the server I used so 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 this way:

 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) {

            }
        }...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.