Atlas_Gondal
Atlas_Gondal

Reputation: 2552

java.lang.IllegalArgumentException: bad base-64 when decrypting image

I am selecting an image using Jsoup parser

Elements images = document.select("img");
String src = images.attr("src");

then using this code to get rid off data:image/jpg;base64

pureImageSrc = imageSrc.substring(imageSrc.indexOf(",") + 1);

Now I have correct base 64 string (i guess) which starts and ends like

/9j/4AAQSkZJRgABAQEASABIAAD/4Vl6RXhpZgAAT...............lbRIluL+9/56L+VFOoqhH/2Q==

finally, I am decoding it and setting inside an image view

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

But getting this exception: java.lang.IllegalArgumentException: bad base-64 What's missing in this?

Upvotes: 5

Views: 11816

Answers (1)

Ryan
Ryan

Reputation: 1883

You are decoding with the flag Base64.URL_SAFE which uses - and _ in place of + and /, your base64 string includes /. Try changing the flag to Base64.DEFAULT

Upvotes: 11

Related Questions