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());
}
});
http://stackoverflow.com/questions/27209319/sending-an-image-file-post-with-retrofit
– Caique Oliveira
Caique, the link doesn’t address my problem.
– Carlos Ximendes
Try for the path photo to send by Retrofit as a
TypedFile
– LeoSantana
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.
– Carlos Ximendes