2

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

    imageGrupo.buildDrawingCache();
    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

rog
  • 363
  • 1
  • 2
  • 6

4 Answers4

3

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.

Ashwin
  • 7,277
  • 1
  • 48
  • 70
0

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);
    v.draw(c);
    return b;
}
  • 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 – rog Feb 14 '16 at 18:25
  • I edited the post, rog. Can you check it please, and if you find useful can you select as "accepted answer", thank you. –  Feb 14 '16 at 18:31
  • Just to make sure, how do you generate the bitmap from an image view? – rog Feb 14 '16 at 18:43
  • I am using this: imageGrupo.buildDrawingCache(); Bitmap bitmap = imageGrupo.getDrawingCache(); but still not working.. no error but no image showing.. – rog Feb 14 '16 at 18:49
  • 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? – rog Feb 14 '16 at 18:58
  • I updated the answer so that you can get Bitmap from ImageView with no problem, that could be the problem as well –  Feb 14 '16 at 19:04
  • hmm.... to the loadBitmapFromView I pass the findViewById(R.id.foto) right? Would you please add the decode function too? So far nothing....Thank you – rog Feb 14 '16 at 20:24
  • just tried... receive a given base64 -> decoded and showed correctly in a image view -> decode the bitmap to a string and get a completely different base64... Of course after saving it to the database I never get the original file back... can´t understand... decoding A into B and encoding B should return A right?.... – rog Feb 14 '16 at 20:53
  • yes I did.. do you have a working project? could you send me in a zip file? regards – rog Feb 15 '16 at 09:23
0

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.

Amir Horev
  • 287
  • 2
  • 3
  • 1
    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... – rog Feb 14 '16 at 18:26
0

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) {
    e.printStackTrace();
    }
    bytes = output.toByteArray();
    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}

Decode:

byte[] data = Base64.decode(base64, Base64.NO_WRAP);
Juan Rojas
  • 8,711
  • 1
  • 22
  • 30