【问题标题】:How to convert a Base64 string into a Bitmap image to show it in a ImageView?如何将 Base64 字符串转换为位图图像以在 ImageView 中显示?
【发布时间】:2011-01-29 13:28:19
【问题描述】:

我有一个表示位图图像的 Base64 字符串。

我需要再次将该字符串转换为位图图像,以便在我的 Android 应用中的 ImageView 上使用它

怎么做?

这是我用来将图像转换为 base64 字符串的代码:

//proceso de transformar la imagen BitMap en un String:
//android:src="c:\logo.png"
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.logo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();
//String encodedImage = Base64.encode(b, Base64.DEFAULT);
encodedImage = Base64.encodeBytes(b);

【问题讨论】:

    标签: android base64 imageview


    【解决方案1】:

    您基本上可以使用其他一些内置方法来还原您的代码。

    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    

    【讨论】:

    • 确保你没有传递“data:image/jpg;base64”并且只传递图像字节..不要忘记将字符串更改为字节.. photoData = photoData.substring( photoData.indexOf(",") + 1); byte[] decodeString = Base64.decode(photoData.getBytes(), Base64.DEFAULT);希望它对某人有所帮助。
    • byte[] b = Base64.decode(previouslyEncodedImage.getBytes(), Base64.DEFAULT);bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);在我的情况下,位图返回 null。如何将 base 64 字符串转换为位图?
    • 已回答如果编码图像字符串是 JSON 响应,只需使用 Base64.URL_SAFE 而不是 Base64.DEAULT
    • 要删除 data:image... 对于 png 和 jpeg 试试这个:String cleanImage = base64Image.replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,","");
    • 精湛的工作就像魅力... +1
    【解决方案2】:

    对于仍然对此问题感兴趣的任何人: 如果: 1-decodeByteArray 返回 null 2-Base64.decode 抛出 bad-base64 异常

    解决方法如下: - 您应该考虑从 API 发送给您的值是 Base64 编码的,应该首先解码,以便将其转换为位图对象! - 看看你的 Base64 编码字符串,如果它以

    开头

    数据:图像/jpg;base64

    Base64.decode 将无法对其进行解码,因此必须将其从您的编码字符串中删除:

    final String encodedString = "data:image/jpg;base64, ....";                        
    final String pureBase64Encoded = encodedString.substring(encodedString.indexOf(",")  + 1);
    

    现在 pureBase64Encoded 对象已准备好被解码:

    final byte[] decodedBytes = Base64.decode(pureBase64Encoded, Base64.DEFAULT);
    

    现在只需使用下面的行将其转换为 Bitmap 对象! :

    位图decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, 解码字节.length);

    或者,如果您正在使用出色的库 Glide

    Glide.with(CaptchaFragment.this).load(decodedBytes).crossFade().fitCenter().into(mCatpchaImageView);
    

    这应该可以完成工作!在这上面浪费了一天,想出了这个解决方案!

    注意: 如果您仍然遇到 bad-base64 错误,请考虑其他 Base64.decode 标志,例如 Base64.URL_SAFE 等

    【讨论】:

    • 我从 json 响应中得到了 base64,使用 Base.64.URL_SAFE 得到了 bad-base64 错误,检查了这个线程并解决了,赞一个..
    【解决方案3】:

    这是一个非常古老的帖子,但我想分享这个答案,因为我花了很多开发时间来管理 @Anirudh 所面临的 NULL 的返回 BitmapFactory.decodeByteArray()

    如果encodedImage 字符串是JSON 响应,只需使用Base64.URL_SAFE 而不是Base64.DEAULT

    byte[] decodedString = Base64.decode(encodedImage, Base64.URL_SAFE);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    

    【讨论】:

    • 我在使用 Base64.URL_SAFE 时遇到 bad-base64 (IllegalArgumentException) 错误。我使用 HTML img 标签验证了 base64 字符串。我在 HTML 页面上看到图像。
    • 嗨@mudit,你知道如何修复bad-base64异常吗?如果是这样,你介意分享吗?我有相同的问题。已经两天了,没有想法。
    • @ito 确保您没有传递“data:image/jpg;base64”并且只传递图像字节。不要忘记将字符串更改为字节。photoData = photoData。 substring(photoData.indexOf(",") + 1); byte[] decodeString = Base64.decode(photoData.getBytes(), Base64.DEFAULT);
    • 我也为“Base64.URL_SAFE”这个词费了很大力气.....我以 json obj 的形式获取...非常感谢
    【解决方案4】:

    要在线检查,您可以使用

    http://codebeautify.org/base64-to-image-converter

    您可以像这样将字符串转换为图像

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Base64;
    import android.widget.ImageView;
    
    import java.io.ByteArrayOutputStream;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ImageView image =(ImageView)findViewById(R.id.image);
    
            //encode image to base64 string
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    
            //decode base64 string to image
            imageBytes = Base64.decode(imageString, Base64.DEFAULT);
            Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            image.setImageBitmap(decodedImage);
        }
    }
    

    http://www.thecrazyprogrammer.com/2016/10/android-convert-image-base64-string-base64-string-image.html

    【讨论】:

    • 发现该网站对于确定我的代码或图像数据是否存在问题非常有用。感谢分享!
    【解决方案5】:

    这是一个很好的示例:

    String base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAA...";
    String base64Image = base64String.split(",")[1];
    byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    imageView.setImageBitmap(decodedByte);
    

    样本位于:https://freakycoder.com/android-notes-44-how-to-convert-base64-string-to-bitmap-53f98d5e57af

    这是过去唯一对我有用的代码。

    【讨论】:

    • 是的,如果不在逗号后拆分,我肯定无法正常工作。我认为这应该是公认的答案,谢谢!!
    【解决方案6】:

    我找到了这个简单的解决方案

    要从位图转换为 Base64,请使用此方法。

    private String convertBitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    

    从 Base64 转换为位图或还原。

    private Bitmap convertBase64ToBitmap(String b64) {
        byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
    }
    

    【讨论】:

      【解决方案7】:

      在 Kotlin 中,您可以使用如下扩展函数:

      fun String.base64ToByteCode() = Base64.decode(this.substring(this.indexOf(",")  + 1), Base64.DEFAULT)
      

      并将其称为如下:

      yourBase64String.base64ToByteCode()
      

      【讨论】:

        【解决方案8】:

        我已经尝试了所有的解决方案,这个对我有用

        let temp = base64String.components(separatedBy: ",")
        let dataDecoded : Data = Data(base64Encoded: temp[1], options: 
         .ignoreUnknownCharacters)!
        let decodedimage = UIImage(data: dataDecoded)
        
        yourImage.image = decodedimage
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-17
          • 1970-01-01
          • 2018-09-07
          相关资源
          最近更新 更多