How to convert image to string in Android? - 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);
}

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 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, I'm not getting the Base64 String as I expected.
iVBORw0KGgoAAAANSUhEUgAAAIcAAAB7CAYAAABet5NoAAAABHNCSVQICAgIfAhkiAAAB/FJREFUeJzt3U1a4lgXB/D/uVQps3IBNuoKZEg56fQKml6BqRU0NdDXITWrBwbtDhp3oDuAicUQVoBYLkBmSndy3gHEDkgggZvkJjm/kR8hXPP8Pbk55IOQkJHVOCi97lnMqBJxFUwHAH4NWp6AMQMPAB4YeFBQ3V/637tJjTcuT2cXVYfJUqBjZq4CqAL4tOFlPRA/M9Mgye1Aca786eyiyqxsZrYAnGpabY8Jty6r25P+9wdN64zV4+fLOhh1AHVsDkJYsW8H7eEY1a6OFVxbATYDR7rXv6QHcKfSb3difp/IRrWr4xLcJvQGYjXiO4A6lR+tW62r1bUi38Y417XOsGa7IG6aEJKftSuLZ9shcJcZF93bYedwjKzGwYfX/SYz/6ljQLsgYMyEhu7/oDDS/OdYRsAYUPauc5OdwjHfl3YQd9mMrudA2UnNSca1yyYBDZi2HYjvnP1/7JPu9fNWL9/mRbMjj48dMP2+zesTMgG4EeeuZl4tbqFvsh2HCUHVt6kikcPxdHZRZZduE5hs6nLjlKeNbf97ghhcNVdi4NtRv9WM8ppI4XisXdgAXSMjG8Rn6JSnlq6AjD9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hboc8vC+PWDg2+7DEqkjvjv60QrVBQ99rexRv9UE8d32oxIGGDr7/9hhF450IfV8xXJ4m00Tpdje9Hh2v41HK8uezi6qrktdyPPgsmSiFFth5hl+kW/BcHjfHijFVtTXiRQR7KjBALa8P8fsjfjLNq8VSeMvlR+trc7wi7xb8Zs94pv+3mUdIk6LT5iOaqdwABIQc+0WDEBDOAAJiHl2DwagKRyABMQQWx2VBNEWDkACkjKtwQA0302w0m93QPgDcm/TpGkPBqC5cnikUZaoWIIBxHQfUl+jTCpIvIZxBQOIqXJ4RlbjoPSy1wVwGuf7FNTQKU+tKJ+VRBVrOAAJSExiDwaQwO2tT7rXz055agG4ifu9CoH4LolgAAlUDr/H2mUHwHmS75kzN5V+y07qzRK9MX6l37LljLKtJRoMIOHK4ZFmWVR62uFRpfJIjdkfyl8gh7ohpBMMIKXK4ZFm2VoTgqqneRvQVMMBSEACxNb1jCL1JzUd3rcHTnl6DDlx2TN0oKppBwMwoHJ4pFkGIKHmVlipVw6PNMtwY1IwAIMqh18Bm2WJ9zDCMKZy+FX6LZsJX9MeRxKY8NXEYACGVg5P/ptl6fUwwjA6HADws3ZlMdxb5OtQdwKCve31JEkxPhxA7nohRvQwwjByzrHs8L49cKCqyH4vJNYzt3TLROXwZLwXYlQPI4xMVA5PhnshvawFA8hY5fDLUC/EyB5GGJmqHH4Z6YVkNhhAhiuHx9xeiNk9jDAyHw7AxF5I9oMB5CQcgDG9kNRP0NEps3OOZQb0QiZKsZWXYAA5CgcAnPS/P8wPdZMOSKaaW2HlKhxAKr2QoVOe5i4YQI7mHKsk0AvpOeVpPWvNrbByHQ4AGH++bBDjrxhWnekeRhi5DwcQSy8k98EAChIOQGcvJB89jDAKEw5ARy+kOMEAcni0ss4OvZAJQf1WpGAABascnojnhWTmzC3dClU5PBF6IcZcfZaGQlYOvzW9kMyduaVbISuH38rzQhK8tZLJCl85PL5eSCF6GGH8H2WNU29+inIcAAAAAElFTkSuQmCC
The real Base64 String it should be this (I use this website to convert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7cQnK0kyCus5Bg7j5DcvSTIrRUTC1gjGOdYSA53FKEWtLydSHL1MFAcrxPEIKEZiFJzDBc9E+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.

Issue in Covert from String Base64 to Bitmap

Code stuff for convert from Bitmap to String Base64
Bitmap thumbnail = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumbnail.recycle();
byte[] b = baos.toByteArray();
String attachment = Base64.encodeToString(b, Base64.DEFAULT);
Code stuff for convert from String Base64 to Bitmap
byte[] encodeByte = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imageView.setImageBitmap(bitmap);
but i get bitmap = null;
I also refer Base64 to Bitmap to display in ImageView
Thanks in advance.
// convert Bitmap to String
public static String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] arr = baos.toByteArray();
imageData = Base64.encodeToString(arr, Base64.DEFAULT);
return imageData;
}
// Convert String to Bitmap
public static Bitmap StringToBitMap(String image) {
try {
byte[] encodeByte = Base64.decode(image, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
You are calling thumbnail.recycle(); after loading bitmap,, either remove it or call before loading bitmap

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

Image from SD card to byte[]

how can i get the byte[] of an image from android emulator SD card?
here's my current code:
File f = new File("/mnt/sdcard/test.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.iv);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
What i want is the byte format of the image how can i do this?
Create a ByteArrayOutputStream then call compress() method of Bitmap so the contents of bitmap will be passed in ByteArrayOutputStream and converted to byte[]
public byte[] bitmapToByteArray(Bitmap b)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
To apply,
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
byte[] bitmapBytes = bitmapToByteArray(bmp);
You can convert Bitmap to byte[] like :
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Folder/" + filename + ".PNG";
Bitmap bmp = BitmapFactory.decodeFile(imageInSD);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Categories

Resources