Wednesday, February 25, 2015

Convert bitmap to String or vice-versa in android

If you want to convert a string to bitmap and bitmap into string than use the below code. please feel free to contact if you face any problem here.


//this method will convert bitmap to base64 String
public static String BitMapToString(Bitmap bitmap) {



String temp = "";
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

byte[] b = baos.toByteArray();

baos.close();

baos = null;
temp = Base64.encodeToString(b, Base64.DEFAULT);

} catch (IOException e) {

e.printStackTrace();
}

return temp;
}


//this method will convert String to bitmap


public static 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;
}
}


0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design