1

I use Base64 system for encode from image to string this code

Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        byte[] image = stream.toByteArray();            
        String img_str = Base64.encodeToString(image, 0);

and decode this code

byte[] decodedString = Base64.decode(decode, Base64.NO_WRAP);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            imageView.setImageBitmap(decodedByte);

but string is too long, very very long. I can't use this way. How can I do short string ?,

Ahmet Atıcı
  • 13
  • 1
  • 4
  • See this [answer](http://stackoverflow.com/a/9768973/2649012) – Phantômaxx Mar 07 '14 at 16:50
  • 1
    Why do you want the string? If your taking an image from device and sending it to a server via HTTP post there are better methods to send the bytes – tgkprog Mar 07 '14 at 16:52
  • Your link is the same my code. I tried it and result is the same. is there another way for convert string ? – Ahmet Atıcı Mar 07 '14 at 17:04
  • History : - Can use zip - part of core java : java.util.zip to compress, but not sure how much it will compress. - It's very unlikely to be able to compress significantly if it's already a jpeg... it's more likely to increase the size slightly. – Jon Skeet 2 hours ago - Most images are already in compressed formats (jpg, png, ...), so that won't help. – Raphaël Lemaire 2 hours ago - Buts its a base 64 string, am certain it will make it smaller. Anwyay should try it before saying so. – tgkprog Mar 07 '14 at 19:47
  • - Well you can't just zip a string - zip applies to binary data, not text. And the output of zip is also binary, but the OP wants text. So to get a string representation of the zipped data, he'd probably apply base64 again... see the problem? – Jon – tgkprog Mar 07 '14 at 19:47

3 Answers3

1

You can't. Images typically contain a lot of data. When you convert that to text as base64 it becomes even bigger (4 characters for every 3 bytes). So yes, that will typically be very long if it's a large image.

You could compress the image more heavily in order to reduce the size, but eventually it will be hard to even recognize as the original image - and may well be quite large even so.

Another way of reducing the size in bytes is to create a smaller image in terms of the number of pixels - for example, shrinking a 1000x1000 image to 100x100... is that an option in your case?

You haven't given us much context, but could you store the data elsewhere and then just use a URL instead?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

I believe the only answer for this is that if you want a shorter string, you should use smaller image

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
0

Depends on the size of the image. A larger image is gonna yield a larger string. Images contain a lot of data. That is why people usually only do base64 encoding for very small images like icons, etc.

You could try reducing the quality of the JPEG compression, but I doubt you'd save much space. Reducing the dimensions (if possible) of the image would probably save some space. Either way, doing base64 on anything larger than a really small gif or png image is almost always counter productive.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36