用于图像/ jpeg的Base64解码;在android中的base64

时间:2015-05-11 12:05:00

标签: android base64 bitmapfactory

我正在尝试将base64编码的字符串解码为android中的位图。 编码后的字符串将以" image / jpeg; base64,"

开头

我使用以下方法:

A

假设我将上述方法称为

Table B

当我运行代码时,它会抛出

  

--- SkImageDecoder :: Factory返回null

  

带有IllegalArgumentException的坏base-64

以下是base64字符串

public static Bitmap decodeBase64(String input)
{
    byte[] decodedByte = Base64.decode(input, Base64.DEFAULT); 
    // also tried using Base64.URL_SAFE , Base64.NO_PADDING

    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

2 个答案:

答案 0 :(得分:19)

字符串“data:image / jpeg; base64,”不是有效的base64编码字符串。因此必须在解码前将其删除。

你走了。

String encodedDataString = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUUEhQUFRQVFBQUFBQXFxQWGBQWFxUWFhQXFRUYHSggGBwlHBQUITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywmHyQsLCwsLC4sLywsLCwsLCwsLCwsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIAJcBTgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EAEUQAAEDAgIGBwQHBwMDBQAAAAEAAhEDIRIxBEFRcYGRBSJhobHB0RMykvAGFBVCUuHxU1RicoKT0jOiskNE4hYjNGNz/8QAGwEAAwEBAQEBAAAAAAAAAAAAAAECAwQFBgf/xAAwEQACAgECBAIKAwEBAQAAAAAAAQIRAxIhBDFBURNhBRQiQoGRobHh8FJx0TLxFf/aAAwDAQACEQMRAD8A5+FSEzArwL64+A3FwpCZgVhiA3FwpCbgUwJC3FwpCaGK8CLDcVCmFO9mrDEWFMThUhPwK/ZosNLM8K8Kf7NTAixaWIwqQnezU9mixUxMKQm4FMKLEKhSE3AqLEBYuFITMKmFAWLhVCZhUhAWLhSEyFITDULhSEcKYUB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ZZAX1RwSGgCwjg4DuutFJs5Ejs/QK6iTclyFOJE533n/kFALe6eEfkmGodojtEeMFBjPPWDHj4p7cg3KdXOsHLY+O6QkEgn3eLZy4LQGkbd8h/eJVNJ1kHhHmhRHqMzwDrbGwkE9/qrZMQCYnVJ8E2uwawN+tZNI0BhjqjfICmUPJMuEk+rQVPo91T3jjjVkCdUrTVY5v3GxtsZ1XuI+bKKJSdM3W8bZna/EbtiNcyZ7k5+jEQSMIdfMTAOwW7goonzqyG6uhwYANhjM3sTfIWyCjpLRhLtc5QIyjWooopWVboChTEAmZdOvXrIAsq0mm5omSQMyDHFRRTNbjg7RVKqTmesMna9x+SoylItn88FFE9EdNiU5OVA0asOLX5tG+PmFppFjx94do1bxN1FFCd8zSq5F1NGc28iM5vw7U2iXRMjfH5BRRPkhtWxrtKc3XLdZMEciJ8UTaw2luuBrv2BRRDlSsWncA6SJPXJ7CDvPZlKaykHZkZTlGedwJ2KlFljyOU9LLnjUY2hYols4nHLqkajkMQvbdtWY6Q7FDyPGRE9iiic8sovYUcMJLcZ9otgADxg8DKSdIbJJbBGy3gYUUXRBKa3RzSuL2YWCm6Ze5picp8BbmhGhBvWa8kDO0W4lUoo0xtqkW5SpOw6DpdYujDYSL75hGKjri1pizYEbZ17lFEpwUY2hQm5yplMc8yScvvdu5NNT8WWTjDe6ZKiiyeWVpdzTwY02W4tuW3i8QPGAVkr1YiYzibWOcSROsc1FEeJJNrsLw4tJ9x7W4RiIBBiD6/oipgOHVE9hy3QVFFnHPKUU3XOi5YYxk0m+QP1IOyJYRkAQQdsCAAgcI90hx2EEH0/VRRdsYI4XNidI0gs6tSROrqlU2hOQvnqHmFaizvematVG0TA7WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k="

encdoedDataString = encodedDataString.replace("data:image/jpeg;base64,","");

byte[] imageAsBytes = Base64.decode(encodedDataString.getBytes(), 0);
imgView.setImageBitmap(BitmapFactory.decodeByteArray(
                imageAsBytes, 0, imageAsBytes.length));

答案 1 :(得分:3)

您不需要字符串开头的数据:image / jpeg; base64,,这实际上不是Base64编码本身的一部分,而是Base64 {{3}用于Web浏览器。只需从/9j/4AAQSkZJRgABAQAAAQABA...开始传递字符串,它就会正确解码。