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

How to convert a Base64 string into a BitMap image to show it in a ImageView?

我有一个Base64字符串,表示一个BitMap图像。

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

怎么做?

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

1
2
3
4
5
6
7
8
9
//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);

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

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


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

解决方法如下:
-您应该考虑从API发送给您的值是Base64编码的,并且应该首先进行解码才能将其转换为Bitmap对象!
-看一下您以Base64编码的字符串,如果它以

data:image/jpg;base64

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

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

现在,pureBase64Encoded对象已准备好进行解码:

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

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

Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0,
decodedBytes.length);

或者,如果您使用的是出色的Glide库:

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

这应该做的工作!在这上浪费了一天,然后想到了这个解决方案!

注意:
如果仍然出现错误的base64错误,请考虑其他Base64.decode标志,例如Base64.URL_SAFE等。


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

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

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


要在线检查,您可以使用

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
    }
}

Android Convert Image to Base64 String or Base64 String to Image