How to convert jpeg image to base64 encoded binary value? - android

In my android application, i want to convert a jpeg image to base64 binary encoded value.
How to acheive this in android ?
Edit
I have encoded my bitmap image into base64 string by following code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] val = stream.toByteArray();
String ba = Base64.encodeToString(val, Base64.DEFAULT);
The output is in the form of string. BUt I need the output as binary value.

if using KSOAP2 for connectivity use this
new MarshalBase64().register(envelope); this is do the magic.

Related

How to upload Image in localhost database in the form of string from android? Not Path only image

I want to upload my gallery image to localhost database in the form of string from android but i did not found any relevant topic. Please guide me
you can convert you Image bitmap to base64 String by this code :`
`ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

How to sent File (Image) along with String parameters in the Body of a POST api (Volley)

Was sending the collection of String Params in the Hashmap in the Api. Now it is required to add a parameter File that has to be an Image.
The body of the POST api looks as below:
Key1, Value1, Text
Key2, Value2, Text
Key3, Value3, File
I have seen many examples of Multipart requests but none solved the issue.
Looking for an approach/example.
NOTE: It's alternative way to sending Image as a File.
You can try converting Image to BASE64 String, and send it as a string.
First, convert your bitmap to byte array:
//can use lower value than 100 for more compression or change compression format as JPEG
ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bAOS);
byte[] byteArray = bAOS.toByteArray();
Then, encode it to BASE64 String:
String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);
Finally put it to your hashmap as a String.

why do i get wrong base 64 string in the code below?

I have the code below to decode a bitmap to a base64 string.
for(String e:paths)
{
String usepath=e.replace("%", "//");
Bitmap m=BitmapFactory.decodeFile(usepath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String bb= Base64.encodeToString(b, Base64.NO_WRAP);
Log.e("Photo", bb);
String usepath prints like
/mnt/sdcard/DCIM/Camera/IMG_20140424_132023.jpg
I have saved the image on my pc and used an online tool to decode it to base64 and i got a long string of around 650kb(after uploading to google app engine) yet the string i get using the above code is like 10% of that and does not display the image .
But i can use the same image path to set an image view and it works like below
Bitmap bm= BitmapFactory.decodeFile(usepath);
holder.imageItem.setImageBitmap(bm);
Any reasons why the base64 encoding failing?
Ronald
Try adding the flag Base64.URL_SAFE to the encoding method.
Consider also that if the image is too large you may not get all the bytes you need in the String (you may try writing to a temp file previous to send the content).

compress and decode picture for max quality

having trouble keeping an images quality while compressing > sending to sever (in BLOB format) > retrieving image > decoding BLOB > then setting bitmap.
When i take the picture on the device or choose to upload one it looks alright on the camera but once I send it to the server and get it back later the quality is garbage.
here is the code I am using while compressing the image:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
and decoding:
String pict = getIntent().getExtras().getString("pic");
byte[] encodeByte = Base64.decode(pict, Base64.DEFAULT);
thanks in advanced!

Image conversion from JPEG to Byte Array in Android

I want to convert JPEG image to Byte Array in android. I am using the below code:
if (PhotoScreen.st_bitPicture != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean b = PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Log.w("test2", "BOOLEAN BOOLEAN BOOLEAN BOOLEAN :"+b);
m_base64EncodedImage = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
}
But it is compressing the image. How can i do without compressing the image?
When you say "compressing", do you mean "convert it to base 64?". You already have a byte array, and then you encode it in base 64. What is your desired output?
PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Creates an in-memory JPEG image (JPEG compressed).
bos.toByteArray()
Creates a byte array with the JPEG data. That's what you want.
Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
Encodes this data to base 64. Just omit that step if you don't want base 64. If you want any other output (like a string with the byte array converted to hexadecimal), then do that instead.

Categories

Resources