5

Can I convert Bitmap to String? And then convert the String back to the Bitmap?

Thanks in advance.

cht
  • 367
  • 2
  • 6
  • 20

2 Answers2

2

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

Community
  • 1
  • 1
Steve
  • 1,439
  • 2
  • 14
  • 27
0

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);
    }
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141