Android base64编码的字符串无效?

时间:2015-02-02 18:25:08

标签: android base64

我在解码Base64编码图像时遇到问题。 例如,我从服务器获得以下字符串:

data:image\/jpg; base64,\/9j\/4AAQSkZJRgABAQEAYABgAAD\/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQ
sJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz\/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA
wMDAwMDAwMDAwMDAwMDAz\/wAARCABFAOIDASIAAhEBAxEB\/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL\/8QAtRAAAgEDAwIE
AwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZW
mNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8v
P09fb3+Pn6\/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL\/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiM
oEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkp
OUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6\/9oADAMBAAIRAxEAPwD5rooor8
DP9oAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA9f8ADPhrTp\/Dmnu+n2
Tu9tGzM0CksSoyScVe\/wCEU0v\/AKBth\/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9ozt3AjONvvXEUVliPHDjavSlRq428Z
Jpr2dHZ6P\/l2dmX\/Qa8EcDiqWNwuSctSnKM4v6zi3aUWmnZ4hp2a2aa7hRRRX5Of1mFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRR
QAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQB\/9k=

我在这里找到的Online Tool正确地解码了String但是代码 我在这里用过:

byte[] imageAsBytes = Base64.decode(imageString.getBytes(), Base64.DEFAULT);

Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

产生错误:

02-02 19:16:56.460: E/AndroidRuntime(18237): java.lang.IllegalArgumentException: bad base-64

问题出在哪里?我想如果它不是一个有效的Base64编码字符串,我提供的链接中的在线工具也无法解码它。

修改

发现了另一个Online Tool

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

但它不知道在哪里。我删除了填充' ='但它没有改变。

1 个答案:

答案 0 :(得分:1)

我不知道这是正确的解决方案,但在我的情况下,我从上面的回复中删除了起始部分data:image\/jpg; base64,,它对我有用。这是不是编码数据部分的数据。