How to convert ImageView image to byte code by using Base64 encode? - android

I am working with an image. In my application I have displayed an image from drawable and set that drawable image to ImageView. When I click on a button I would like to encode the ImageView image to byte code by using Base64.
I have implemented code as follows:
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.person);
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((TextView)findViewById(R.id.textView1)).setText("Get((ImageView)findViewById(R.id.imageView1)) image Base64.encode() here");
}
});
How can I get the encoded imageView1 image to byte code?
Can anybody please help me.

Try this...
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.images);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
System.out.println("byte array:"+image);
String img_str = Base64.encodeToString(image, 0);
System.out.println("string:"+img_str);
Now set that string to your Textview as
tv.setText(img_str);

Look at this code,
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.person)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bMap .compress(Bitmap.CompressFormat.PNG, 100, baos);
//bMap is the bitmap object
byte[] b = baos.toByteArray();
String encodedString = Base64.encodeToString(b, Base64.DEFAULT)

use this
public String encode(Bitmap icon) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte[] data = baos.toByteArray();
String test = Base64.encodeBytes(data);
return test;
}`

Related

Converting bitmap into Base64 not encoding correctly in android.. I not getting image back when decoding

This is my code and I tried lot of code varieties already...
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
return encodedImage;
}
You can try following code to encode and decode image:
//Compress img and save
private String encodeBitmapAndSave(Bitmap bitmap1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}
//Decompress img
private Bitmap decodeFromFirebaseBase64(String image) throws IOException {
byte[] decodedByteArray = Base64.decode(image, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByteArray, 0, decodedByteArray.length);
}

How to convert image to string in Android?

It's my MySql DB
Its encoding:-
public String convertBitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr, 1);
return imageStr;
}
This is decoding:-
String img=o.toString();
byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Thank you.
It could be done by
private String getBase64String() {
// give your image file url in mCurrentPhotoPath
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// In case you want to compress your image, here it's at 40%
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
And for Decode
private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) {
// Incase you're storing into aws or other places where we have extension stored in the starting.
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
}

Convert Image to Base64 String Android

I am able to get an encoded String using the following code :
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.NO_WRAP);
}
BitmapDrawable drawable = (BitmapDrawable) imgPreview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
encodedImage = encodeToBase64(bitmap, Bitmap.CompressFormat.JPEG, 100);
I get the following as String
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCADmAZgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDjNH0CSxvdTs4LdYEaa0vBGq7FaO4j8hiMscAS2rOp24VsEMMknVls5Yku1iR3QqoOccbTKdpOD+7AZSAFxksdxCsx/TOT9mX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0XJk1S80vTIY5FCvGsl/bRNs3gk+YkUpkx8zBn+UgE125dF1MZQp33qw/Cbbf3Rv83q2mzixiUMPiJf9O5bLtzb6vyXk0tk9cH9iPRGku/HV+zSrHa6b4VtcgHYcR3V26GQgKhInSTgHjgAkFh/SV8KI28QfsjeJNPZxJD/AMIv4kgixuy2JNbhGzBHyR+Wq/3gUHVChP4HfskaUmk/DTxXf7PNk1rxpf20MkaANNY6Hp+n6eSSQuEWaOdIiuSHEwLBo2U/vT+y4s0/7OXiPR3djPZWevRPCzZk/wBIluLxsg52iRrqV1VsMEZSy5LE/c4Spz5lj7O96MoJ9PdUl16O731Wl7NnymIpcuXYJvRxq0pPX+abu7W62X33vdM/Mi9uQ+nG6Mwjt7a0F3OUwI4rdFbIchiSURDIUySSWO4cmuH+GdlNqGn694xvIAtz4p1RHsRI8pkj02Jmi0qI+ZyqiESXTBTtKEHaFDVzfjDWZ4fCth4UsWf+1PGOpLo8YTBkg0wXEzatcgDJURxDaC4JVXHQ5avc/DNhEYrCyto1FnpqCCNAP+ecbwmQ8DCxwggdBiYBSVJNfJYlezjUSbi3Jx6/z3e99euvTq3qfT4f3oxSStCMU2r76J9XZ6X6fFLZtsvhAbWNJDlWmjlQcZkSGRXgRwRtCmVYpZPl5iWYL8zAnq7hfsmiW1szN590itclhg72JdiSVUnIUKwPVt4zuOBmqsVxeyEyOINOTftITyyqZAwSOWZ1ZTg52IRjg5bKLnUZYUjcl7mWC2tvkLNH5zOXb5jgmOIM+eijeyqSGY+RGPNfyt+fr21/Dc75JaJ3trb5Nd393m1ro7uuLs6J4Xur6ND5mqTfZoFjAaaS0DbGjUOw2veTbIAUIKpPuUAxsTL4at40jmFxGsgtVa+vZB8wvNSld5JdwJG95LotHBGCwSNURCylmrnfFOtQXfiu30W2Rp
But when i try to decode it using http://freeonlinetools24.com/base64-image nothing shows.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
byte[] byteArray = byteStream.toByteArray();
String baseString = Base64.encodeToString(byteArray,Base64.DEFAULT);
This worked for me.
I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so the broken string didn't decode the image for me.
So when i passed the encoded string directly to the decode function the image was visible.
I checked an image with given link http://freeonlinetools24.com/base64-image and it works fine. I think the reason for getting wrong output is your string. Encoded string always ends with == . Check your encoded string.
Example Code
Encode Image
BitmapDrawable drawable = (BitmapDrawable) imgPreview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
encodedImage = encodeToBase64(bitmap, Bitmap.CompressFormat.JPEG, 100);
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
Decode Image
Bitmap bitmap = decodeBase64(encodedImage );
img.setImageBitmap(bitmap);
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input,0);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
EDITED
see my string..
iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAbhUlEQVR42u2df5BV5XnHn7u7yu4CsoATcRRZbWSxMxHWfxRTcdUwNJqJpBpNqlO22lE7YkGTaFKMXmaw0bRVbDFgKrj8kTGVZgSjSR1RVwXRZopLmk4EE10KjZqKLCi/NGrf595zlrNnz7nn53veH+f7mTmc/XH33vcu+3zf9/nxPm+FgLEsXfLFDnGb5Xza49w7nYvh78/M+DLbxDXkfDzoXEy/cx+4866fDyV7SqALFdUDANEIQ2cj76S6sfPFhn2+6nH5eJ7qQjHgXINCGAZUDwo0BgKgGY6x99BRY886g6uGVxCuKPRDFPQCAqAYYfA9VDd4vnSb1WXBq4V+qgtCv+rBlBkIQMF4Zvj5VB6Dj4IFYT1h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s3Yaw1E8GBCAhpmYNbO82hKh+OiAAKXGak/aSQXECG7sNsX9fqdAa05px6gIEICOOe9BLdfdA+1WBDalCZ7Zf2dzctBLL/GxAAHLEqS5kIeA9B9rGCkzsNuT49k+J2f4fylq1JwMIgCScjUfupaUY6F5C7DH6x8q0QadIIAAF4IhBD9XFQCs3QbduQ7y8F9e/C6N/FkYvHwhAwThuQg/VxUCLjUiquw05G3J+Kj7ciOV9sUAAFONkE9xLqSAU1W2IDV786W0Ss/wziN6rBQKgGZ4VwiznKrTNed7dhriNtrh+JYx9gDDDawcEwAAcUeiko6LAqUdpq4U03YbqszrtF39SvxTGvlV8/FsYu/5AAAzGqUGY5Xza49w7nYvh76daQbjdhj788GMxg9O++lcru8Q/O2sfieU735ubm15BLt5c/h/s/pXAGnPaugAAAABJRU5ErkJggg==
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
Encode/Convert image to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

How to convert Bitmap to Base64 String

I am developing an android application which uploads Base64 String of image into web server. Therefore, I have used the following code to convert a Bitmap into Base64 String.
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteFormat = stream.toByteArray();
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
For example, I need to convert this image.
After the conversion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+tmKq56v7qmLecR5kdeVeEsOJEicYLQKhwHId4U5iHgwWl1WSksMfRS80scZzptMMFpPDiNBikEZEsSvtr7A8JYcRoKcc4IMcR69geFtq1CvOxjzf3ys8GGdODC8w1ECZI5z2oqWXYDhfVmpWGaom5ni/NYW3eicdfWPdboxPr8wXFEmAIzOk8NIEHqRmKa6q5VxeNDJYSQIvdAbtLrhgtFbchgJgmGZXfTGmfS5DWivcACQg2B4G24FvayUlhgy4FJhFlKIfBj1DUYQyWEkCKapHQ23xCSHkSCpD8vmIYERVHKUUoSq85SGZZ3PMEQlRylFyKi7RMC4CxGMYJPDSBAyLeZhWW8zDPFwMCBUhywia3WLWxOD3jE6eDgYkJhmIUHMMMTWHBU1CBk5UvJnIVspYIhJDiNBJM9CgpphRJMcRoJInYWspYEhLjlKKUJVvoRZyDzUVjWq5CilCBl+BzCQHIcShMwPcRYS9AwjCTgYEKpDQpqFiAcjGjgYkBBmIb1foIOao7oG6XsWUgy3VrF4Gg0cDMhO9XMTlajhVpJwMCBdz0KiBCOqmsNSh1BR6HMWsuYaYx+jf1HDwYDQY0F+efjRomcYgON/QOgkPgIMwGEDhOqQNmYhUcwwAMdHQJrOQpIBI8pu5UgnU3cWQjOMrymBkVxyGAlyynUhYq7cQnK0kyCus5Bg7j5DcvSTIrRUTC1gjGOdYSA53FKEWtLydSHL1MFAcrxPEIKEZiFJzDBc9E+AAQBQ99BNOprhRAAAAABJRU5ErkJggg==
What is the problem in my conversion ?
Thank you !
public static String encodeTobase64(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
use the following snippet to convert bitmap to byte array:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
then to encode base64 from byte array use the following statement:
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
public String getBase64ImageString(Bitmap photo) {
String imgString;
if(photo != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] profileImage = outputStream.toByteArray();
imgString = Base64.encodeToString(profileImage,
Base64.NO_WRAP);
}else{
imgString = "";
}
return imgString;
}
Pass your Bitmap in the parameters you will get your image string.

compress bitmap to string android

Hi guys I have a problem when I go to encode the image to a bitmap and then do a comparison with the decode an encoded image with tools via the Internet but does not give me the same decoding and can not figure out why.
String path = "/sdcard/bluetooth/bluetooth.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
I used the below for encoding and decoding and it works fine for me
Encode
public static String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("LOOK", imageEncoded);
return imageEncoded;
}
Try the below for decoding and set the resulting bitmap to imageview and check with the original.
Decode.
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

Categories

Resources