Convert Bitmap to String

Is it possible to convert Bitmap to String? And then convert the string back to a bitmap?

Thanks in advance.

+5
source share
2 answers

This code shows what you want to convert to a string:

This shows how to do both: How to convert a Base64 string to a BitMap image to show it in ImageView?

And this shows the transition back to .bmp: Android code to convert base64 to bitmap

Google is your friend ... you should always ask your friends if they know the answer first :)

Steve

+3
source

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);
    }
0
source

All Articles