Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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?

like image 646
Atlas_Gondal Avatar asked May 25 '17 13:05

Atlas_Gondal


1 Answers

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

like image 102
Ryan Avatar answered Nov 06 '22 23:11

Ryan