首页 文章

如何将Base64字符串转换为BitMap图像以在ImageView中显示它?

提问于
浏览
146

我有一个表示BitMap图像的Base64字符串 .

我需要再次将该String转换为BitMap图像,以便在我的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);

4 回答

  • 50

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

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

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

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

    data:image / 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 对象! :

    Bitmap decodingBitmap = BitmapFactory.decodeByteArray(decodingBytes,0,decodingBytes.length);

    或者如果你使用的是伟大的图书馆 Glide

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

    这应该做的工作!它浪费了一天时间来实现这个解决方案!

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

  • 7

    这是一个非常古老的线程,但我想分享这个答案,因为我需要花费大量的开发时间来管理 NULL ,如@Anirudh所面临的那样 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);
    
  • 14

    要在线查看,您可以使用

    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

相关问题