Reputation: 367
Can I convert Bitmap to String? And then convert the String back to the Bitmap?
Thanks in advance.
Upvotes: 5
Views: 16550
Reputation: 1459
This code appears to be what you want to convert to a string:
This shows how to do both: How to convert a Base64 string into a BitMap image to show it in a ImageView?
And this one shows converting back to a .bmp: Android code to convert base64 string to bitmap
Google is your friend... you should always ask your friends if they know the answer first :)
Steve
Upvotes: 2
Reputation: 3047
Any file format
public String photoEncode(File file){
try{
byte[] byteArray = null;
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024*8];
int bytesRead =0;
while ((bytesRead = bufferedInputStream.read(b)) != -1)
{
bos.write(b, 0, bytesRead);
}
byteArray = bos.toByteArray();
bos.flush();
bos.close();
bos = null;
return changeBytetoHexString(byteArray);
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
private String changeBytetoHexString(byte[] buf){
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
Upvotes: 0