Binary Request Body in kotlin - android

How Can i set image from imageview in binary body in API endpoint?
I need to get image from an ImageView and put it into a request body that expects a binary value (jpg image)
How can I do the bellow API calling in kotlin?
This is the call in postman

There isn't enough context in your question, eg example code. But try the below:
//get drawable from your imageview
Drawabel drawable = imageVIew.getDrawable();
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
//convert image to binary
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
byte[] imageInByte is what you asked for, but I have also added conversion to stream in case you end up needing that.

Related

Dart/Flutter Image library decode Bitmap fails

I am trying to read a bitmap generated on the platform site
here is the code I use to generate a byte array in android:
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
return byteBuffer.array();
When I try to decode it with the image library it fails (dart code):
Image img = decodeBmp(bitmapData);
The method 'getBytes' was called on null.
bitmap data is not empty and looks proper for me
If I instead compress image on the android side to png or jpg than decodeImage (or -Png or -Jpg) works
but I dont want to use bitmap.compress
this code would work with compress
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bufferedBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageData = baos.toByteArray();
return imageData;

Pass Drawable to iTextPdf instead of an Image - Android

I am using iTextPdf to create a check-in application. I've added the an image already using the image object :
imagePath = "/sdcard/Mugshot.jpg";
Image image = Image.getInstance(imagePath);
image.setAbsolutePosition(165f, 465f);
image.scaleToFit(290f,290f);
document.add(image);
I would prefer to add the image in the same way before adding any content, as I expect any subsequent stuff would write over existing stuff.
As per the API Documentation of iTextPdf you can also contruct using byte[] array
Convert drawable to byte[]
Drawable d = getResources ().getDrawable (R.drawable.your_drawable)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapData = stream.toByteArray();
Then
Image image = Image.getInstance(bitmapData);

Encoding and Decoding image

I want to insert image into 'MYSQL' database using json and retrive the inserted image into 'ImageView'.I done encoding as well as inserting image but not able to do the decoding and set the image to 'ImageView'.
//Encoding the imageView
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.imageVieweditprofileuserphoto);
BitmapDrawable drawable = (BitmapDrawable) imageofuser.getDrawable();
bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str= Base64.encodeToString(byte_arr, 1);
//Image after encoding at server side(Database MySql)
iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAAA3NCSVQICAjb4U/gAAAQPElEQVR4
nO1c6W9cR3Kv6u735s0Mh4dFUjy0luUj9lo+13YQJIGdYAUs8ilAPuRPyJ+XAPFugBjYrLO2s+s1
1rZsyxJ18BDP4XDueWcflQ/9ZjjiIUp8b0jCqx9IDTVHv+rfVFVXV1c9rFar8AzHg523ABcdzwg6
Ac8IOgHPCDoBzwg6Ac8IOgHP
//Decoding the imageView
imageofuser=(ImageView)view.findViewById(R.id.imageVieweditprofileuserphoto);
byte[] imagefromserver=Base64.decode(Utility.userimage, Base64.NO_WRAP);
Bitmap bmp=BitmapFactory.decodeByteArray(imagefromserver, 0, imagefromserver.length);
imageofuser.setImageBitmap(bmp);

ParseFile on Android

I'm using the Parse SDK to build my app.
I can easily get a Bitmap out of the gallery by using Intents. I want to use them as a profile picture for my users.
However, in order to upload it I must convert it into a byte array. Also, when I download it it comes in the form of a byte array, and I must convert it back to a Drawable.
In order to convert it into a byte array, I'm doing this:
public static byte[] bitmapToByteArray(Bitmap bmp)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
I cannot find how to convert this byte[] back into a Bitmap without having to save it first. How can this process be achieved?
Just do the following:
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

How to get a byte array from a drawable resource ?

I would like to get a byte array from an jpeg image located in my res/drawable file ?
Does anyone know how to do that please ?
Drawable drawable;
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Get a bitmap decodeResource(android.content.res.Resources, int)
Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer.
http://developer.android.com/reference/android/graphics/Bitmap.html
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.JPEG /* FileType */,
100 /* Ratio */, stream);
HTH !

Categories

Resources