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