Convert Base64 String to Bitmap

Introduction
     In android, Normally we send and receive data or image in the form of string.if we have receive image as base64 string you cant set into imageview.So here i made a simple function that you need pass base64 string and it will return a bitmap.later you have set into imageviews.

demo

Using this code

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

Here is the procedure for converting string to bitmap but string should Base64 encoding

MainActivity.java
package com.example.base64tobitmap;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String base="****Base64 string values of some image******”;
byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.imageView1);
image.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);

}
}


Previous
Next Post »

7 comments

Write comments
Anonymous
AUTHOR
February 22, 2015 at 2:58 PM delete

I really like it whenever people come together and share opinions.

Great site, stick with it!

my site ... pirater un compte facebook

Reply
avatar
Anonymous
AUTHOR
June 19, 2015 at 3:21 PM delete

Good Tutorial for the beginners, Thank you.

Reply
avatar
amir hide ip
AUTHOR
October 26, 2015 at 10:05 PM delete

thx a lot brother this helpe me so much i appreciate this

Reply
avatar
kopite
AUTHOR
March 30, 2016 at 8:09 PM delete

how to implement it in Listview?

Reply
avatar
Kalidoss
AUTHOR
March 31, 2016 at 10:19 AM delete

Create listview with using custom adapter and then set that bitmap values into imageview thats it.

Reply
avatar
Unknown
AUTHOR
May 18, 2017 at 2:55 PM delete

what if its a large image and the String datatype in java cant accomodate the base64 image string. is there anyway to do that???
Thanks

Reply
avatar