encode image to base64 is not equal to original

base64 to image
base64 encode javascript
base64 to binary
convert ascii to base64
file to base64
base64 encode php
encode username and password base64
base64 encode private key

I am trying to encode/decode an image view and I looked at more than 50 examples but it still does´t work.

I did a simple thing to start with:

1) i am using the simple base 64 data from http://codebeautify.org/base64-to-image-converter#

2) i put it into a blob field of a mysql db (hosted in a remote server)

3) in android I get it correctly via a web service, decode it and show in an image view

public static Bitmap decodeBase64(String input)
    byte[] decodedByte = Base64.decode(input.getBytes(), Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);

(so far so good)

4) i am encoding what is in the image view (placed in step 3) and generate a base64 code using

    Bitmap bitmap = imageGrupo.getDrawingCache();
    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
    byte[] image=stream.toByteArray();
    finalImagemStr = Base64.encodeToString(image, 0);

5) if i compare the generated base64 with the one I received in 3 they are not the same.

They should be right?

6) when placing the base64 string generated in 4) in the remote database and reading it again it says

  java.lang.IllegalArgumentException: bad base-64

I am going crazy at this point... Any ideas?

Thanks Regards

Try my solution which explains how to encode bitmap to base64 and decode base64 back to bitmap successfully. You can skip the scale image part of the code if you require the exact original image.

Why does a base64 encoded string have an = sign at the end , with one or two = if they are required to pad the string out to the proper length. 2- As a short answer : The 65th character ("=" sign) is used only as a complement in the final process of encoding a message. Unfortunately, images cannot be combined and delivered together in their binary form. Takeaways: The binaries are always smaller. Sometimes Gzipping makes the files larger. Gzipping the base64 version brings the filesize close to the size of the original binary, but this ignores the fact that the binaries get Gzipped as well.

I don't understand why do you encode and decode again when you already have the bitmap. And by the way in PNG format the 90 is ignored in the "compress" method.

How to check whether a string is Base64 encoded or not, How do I know if a string is base64 encoded? World's simplest image base64 encoder. Just select your JPG, PNG, GIF, or BMP picture or drag & drop it below, press Convert to Base64 button, and you get a base-64 string. Press button, get base64. No ads, nonsense or garbage. Works only in modern browsers.

Base64 was way too long text and wasn't able to fit in my log console, and I spent an hour working on that why not working, it turn out that the generated texts were same but it didnt output the whole text, and only the part of it, this might be the case here

update to OP's code request

thats the method I use currently, it is solid and working without any problem.

public static String bitmapToBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 30, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

    return encoded;

To make sure you are getting the 'safe' Bitmap from the imageView, getDrawableCache is tricky. Use this.

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    return b;

Know file size with a base64 string, in your system has some specific limitation or identification. Base64 decoding is the opposite of Base64 encoding. In other words, it is carried out by reversing the steps described in the previous section. So, the steps of Base64 decoding can be described as follows: Each character in the string is changed to its Base64 decimal value.

This solution worked for me I just changed Base64.DEFAULT to Base64.NO_WRAP The output will be the same as if you use an online converter like http://freeonlinetools24.com/base64-image

private String encodeFileToBase64(String filePath)
    InputStream inputStream = new FileInputStream(filePath);//You can get an inputStream using any IO API
    byte[] bytes;
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        while ((bytesRead = inputStream.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    } catch (IOException e) {
    bytes = output.toByteArray();
    return Base64.encodeToString(bytes, Base64.NO_WRAP);


byte[] data = Base64.decode(base64, Base64.NO_WRAP);

Base64, In computer science, Base64 is a group of binary-to-text encoding schemes that represent The padding character is not essential for decoding, since the number of of Base64-encoded binary data is equal to 1.37 times the original data size + 814 <image xlink:href="data:image/jpeg;base64, JPEG contents encoded in  ToBase64String(Byte[], Int32, Int32, Base64FormattingOptions) Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.

When to base64 encode images (and when not to), Image sprites are a concept taken from video games: the idea is to cram a ton Is base64 encoding with gzipping roughly equal to the original  Base64 encoding of large files. Base64 encoding converts triples of eight-bit symbols into quadruples of six-bit symbols. Reading the input file in chunks that are a multiple of three bytes in length results in a chunk that can be encoded independently of the rest of the input file.

PC Mag, MIME ENBDDING METHODS g Not encoded; message contains 8-bit characters and with equal signs followed by two-digit BASE64 ENCODING Conversion table 5 the Internet in mail messages that are compliant with the original specification. A message containing a J PEG image might have a header like this one:  The ratio base64/original converges to 4/3 for large sizes, but for small data the ratio can be higher (e.g. time 4 for a 1 byte original message). As base64_encode() encodes in MIME compliant way, you have to count in addition the newlines (at least one every 76 base64 chars). For decoding it's just the opposite.

Microservices for the Enterprise: Designing, Developing, and Deploying, The base64 encoding defines how to represent binary data in an asCII string format. If the length of the original binary data is not an exact multiple of 24, then we data to make its length equal to the very next multiple of the 24, which is 240. If we try to send a base64 encoded image as a url query parameter and if the  Base64 image encoding should only be used for small images Base64 encoding is simply an encoding mechanism - there is NO inherent limitation based on filesize. If it is indeed true that the attached util chokes on big files, then it is a limitation / bug in the tool, not a limitation of the base64 specification.

  • I decode when I receive and then I decode to send to the server. In this case it is the same file because i am just testing. When this works I can load any file from the gallery and send to the server. But if I can´t encode this one I can´t encode any...
  • Thanks. Yes, that occurred to me. But in the first 50 characters i notice some differences already... But still I send that to the server and when reading back it does´t decode. So something is wrong in the process. So you have any working example? Thanks
  • I edited the post, rog. Can you check it please, and if you find useful can you select as "accepted answer", thank you.
  • Just to make sure, how do you generate the bitmap from an image view?
  • I am using this: imageGrupo.buildDrawingCache(); Bitmap bitmap = imageGrupo.getDrawingCache(); but still not working.. no error but no image showing..
  • receiving base64 string starting with "iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlAAAQN0lEQV". When creating again base64 from the correspondent bitmap it generates "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABsSFBcUERs"..... too different don´t you think?