0

Looks good?

public static Bitmap stringToImage(String base64) {

    byte[] decodedString = decode(base64, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}

this code returns null, if my base64 string starts with "data:image/jpeg;base64,". but if I remov this prefix - this code works fine! How to solve this problem?

I tested my base64 string and it works fine (comment 1 How to display Base64 images in HTML?)

Community
  • 1
  • 1
monyag
  • 800
  • 3
  • 9
  • 27
  • I can to remove this prefix manually, but whether there is a class/method that will automatically remove it? – monyag Jul 02 '12 at 05:14
  • using the regexp - Pattern.compile("^(data:image/.+;base64,).+"); Android does not contains a mime-type strip method :( – monyag Jul 02 '12 at 09:29

2 Answers2

1

Unless I'm confused, "data:image/jpeg;base64," is not a valid part of a base64 string. If that's the case, it's really no wonder it's not decoding correctly. Just remove it from the head of the string before you decode if it's causing problems.

Geobits
  • 22,218
  • 6
  • 59
  • 103
0

u may use before passing the String to function

    String base64="";
base64.replaceAll("data:image/jpeg;base64,", "");

pls change given string according to ur string

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45