Android base64编码、图片转换

1 将base64编码转化成图片

(1)类似base64流的图片解析并展示:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcH Bw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAPUAooDAREAAhEBAxEB/8QAHQAAAQUBAQEBAAAAAAAAAAAAAgABAwQFBgcICf/EAE0QAAEDAgQEAwUGBAUCBAQEBwEAAgMEEQUSITEGQVFhEyJxBxQygZEjQqGxwfAVUtHhJDNDYnII8RZTgpIlNETCF2Oy0iZUc4OTouL/xAAaAQEBAQEBAQEAAAAAAAAAAAAAAQIDBAUG/8QAMxEBAQACAgICAgIBAwMEAgIDAAECEQMhEjEEQRNRIjJhBRRxQoGRIzNSsQahFtEVQ+H/2gAMAwEAAhEDEQA/APSwPovG+kKyaDgKhwinsgIBRRs3Cs9oEg3XSMlZVD2QKyGj2QPZArIFZEIhA1rJoJNKSaQrJoMRzVQ1lNKVk0FZUNZQK11QxCIrSDzlVAEISGITsMQnYaybU1imwkCtooHsgYhSQMQgVkCRSVQ1t0DW0QK3ZA1kNlYKhH1SBBpJ0CJY0W55cvlsALLWMKevj8RjWnQjZay7SM59O9vK/oudioiLaWU0prIgbIGIQMRqmgxCnYYpIBsm1MQUE1INJP8AiVqJVQhQLkiBKKYogCiBKKY9VAxQoCiGKACgYoBKAToEKEoBOpVoAogHKVYByJQOCbIBQ09NAXN6D2QPZFOApsPZNqIBEGweZWAea6sEgcIFZArIHQKyBWKBW0V2hWU2piFU0Vk2pWQLom00YoGsh2QQJArIGQVn/EqyAhUNZRTdkQkU1kCsVE0ayKQ0QKyBWTYVlIGtoqGI+aBEJAypsipo2VlUpiLa8kW09Nknfljka4jcApqm40oKUNFyFuYokkkigb5it9RGfNUulkuBYDZc7n2ujtl6pvadnc2N+4CuoIn0oOrHfVTxXaCSF7NwVnVhURHVZ2gVVMUDFQDzRTFEWaSN9nafENFqFVJo3RvLXizgpQCiAOiqhKBiFNpoJQCUAlECVFMVUCUDFAJQCUAlABCATsgjJQ9AKIBymwCNaen2XN22cBA4sgSNCCFOAoDYNVqIGy6MHsgeyBIEgcboG5oHQKyBKhEIGsmkJNKShsrKkMQiGtqi7KyBWRAkIqvIPMqyCybOjWTaUJCKVkoSWmi9E0aKylCsgVkDWQN+KBdkCAQNZA1lTRFDY4IzI42F7C5CsmwDrPOVo3NiCr4/Q4vFoazCONaKehe4QzOAkJWjIySBigRQMgSBggSBuSBFChOyJs1yro2/9k=

(2)将Base64数据流转换为可展示的Bitmap

byte[] decode = Base64.decode(firstInfo.img.img.split(",")[1], Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
ivCode.setImageBitmap(bitmap);

注意:含有“data:image/*;base64”的头的编码,在decode的时候就要去掉,否则是无法还原成功的。
去掉头部

firstInfo.img.img.split(",")[1]

再将bitmap转化成图片!

2 将Base64转成Bitmap

    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data.split(",")[1], Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

你可能感兴趣的:(Android控件,android)