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

时间:2022-12-02 23:15:54

I have a Base64 String that represents a BitMap image.

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

I need to transform that String into a BitMap image again to use it on a ImageView in my Android app

我需要把这个字符串转换成位图图像在我的安卓应用程序的ImageView上使用它。

How to do it?

如何去做?

This is the code that I use to transform the image into the base64 String:

这是我用来将图像转换成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 个解决方案

#1


270  

You can just basically revert your code using some other built in methods.

您可以使用其他内置的方法来还原代码。

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

#2


39  

To anyone who is still interested in this question: If: 1-decodeByteArray returns null 2-Base64.decode throws bad-base64 Exception

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

Here is the solution: -You should consider the value sent to you from API is Base64 Encoded and should be decoded first in order to cast it to a Bitmap object! -Take a look at your Base64 encoded String, If it starts with

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

data:image/jpg;base64

数据:图像/ jpg,base64

The Base64.decode won't be able to decode it, So it has to be removed from your encoded String:

base64解码无法解码,所以必须从你的编码字符串中移除:

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

Now the pureBase64Encoded object is ready to be decoded:

现在,pureBase64Encoded对象准备好了:

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

Now just simply use the line below to turn this into a Bitmap Object! :

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

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

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

Or if you're using the great library Glide:

或者如果你使用伟大的图书馆滑翔:

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

This should do the job! It wasted one day on this and came up to this solution!

这应该能完成任务!它浪费了一天的时间来解决这个问题!

Note: If you are still getting bad-base64 error consider other Base64.decode flags like Base64.URL_SAFE and so on

注意:如果你仍然有坏的Base64错误,请考虑其他的Base64.decode标志如Base64。URL_SAFE等等

#3


14  

This is a very old thread but thought to share this answer as it took lot of my development time to manage NULL return of BitmapFactory.decodeByteArray() as @Anirudh has faced.

这是一个非常古老的线程,但我认为应该共享这个答案,因为我花了大量的开发时间来管理BitmapFactory.decodeByteArray(),就像@Anirudh遇到的那样。

If the encodedImage string is a JSON response, simply use Base64.URL_SAFE instead of Base64.DEAULT

如果codeendimage字符串是JSON响应,只需使用Base64。URL_SAFE代替Base64.DEAULT

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

#4


4  

To check online you can use

你可以上网查询

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

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

You can convert string to image like this way

您可以将字符串转换成这样的图像。

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

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

#1


270  

You can just basically revert your code using some other built in methods.

您可以使用其他内置的方法来还原代码。

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

#2


39  

To anyone who is still interested in this question: If: 1-decodeByteArray returns null 2-Base64.decode throws bad-base64 Exception

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

Here is the solution: -You should consider the value sent to you from API is Base64 Encoded and should be decoded first in order to cast it to a Bitmap object! -Take a look at your Base64 encoded String, If it starts with

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

data:image/jpg;base64

数据:图像/ jpg,base64

The Base64.decode won't be able to decode it, So it has to be removed from your encoded String:

base64解码无法解码,所以必须从你的编码字符串中移除:

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

Now the pureBase64Encoded object is ready to be decoded:

现在,pureBase64Encoded对象准备好了:

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

Now just simply use the line below to turn this into a Bitmap Object! :

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

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

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

Or if you're using the great library Glide:

或者如果你使用伟大的图书馆滑翔:

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

This should do the job! It wasted one day on this and came up to this solution!

这应该能完成任务!它浪费了一天的时间来解决这个问题!

Note: If you are still getting bad-base64 error consider other Base64.decode flags like Base64.URL_SAFE and so on

注意:如果你仍然有坏的Base64错误,请考虑其他的Base64.decode标志如Base64。URL_SAFE等等

#3


14  

This is a very old thread but thought to share this answer as it took lot of my development time to manage NULL return of BitmapFactory.decodeByteArray() as @Anirudh has faced.

这是一个非常古老的线程,但我认为应该共享这个答案,因为我花了大量的开发时间来管理BitmapFactory.decodeByteArray(),就像@Anirudh遇到的那样。

If the encodedImage string is a JSON response, simply use Base64.URL_SAFE instead of Base64.DEAULT

如果codeendimage字符串是JSON响应,只需使用Base64。URL_SAFE代替Base64.DEAULT

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

#4


4  

To check online you can use

你可以上网查询

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

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

You can convert string to image like this way

您可以将字符串转换成这样的图像。

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

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