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