In android, Normally we send and receive data in the form of string.So if we have image in the Bitmap form then we can not send it to server.So here i made a simple function that you need pass bitmap and it will return a string.
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream ByteStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
byte [] b=ByteStream.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
Here is the reverse procedure for converting string to bitmap but string should Base64 encoding
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream ByteStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
byte [] b=ByteStream.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
Here is the reverse procedure for converting string to bitmap but string should Base64 encoding
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
Very useful post for developers.Thanks for sharing it.
ReplyDeleteandroid development
Your welcome Keep visiting....
ReplyDeleteit is not converting into bitmap and yes its shows the encoded string
Deletethank u so much ankit its run!!!! oh my God you are genius thank u so much!!!!!!!!!!!!!!!!!!!!!!
ReplyDeleteYour welcome..Keep visiting...
ReplyDeleteMy string contain 3 dots(...) at the end of it. why does it happen.?
ReplyDeleteit not work for the string why? i give it 64base string but it not return me a bitmap all time i got null
ReplyDeletemy 64base string is-->aGVsbG8=
you can try this free online service to convert image to base64 string and convert base64 to image online.
ReplyDeleteBitmap returning Null
ReplyDeleteBitmap returning null
ReplyDeleteBitmap returning null
ReplyDeleteAm a Mobile App Developer and this post really helped me. Thanks for sharing this.
ReplyDeletei make signature and try to save in sqlite dabase and display it again.
ReplyDeleteCan i use this method??
Bitmap returning null
ReplyDelete