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:
Here is the procedure for converting string to bitmap but string should Base64 encoding
MainActivity.java
<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)
);
}
}
7 comments
Write commentsI really like it whenever people come together and share opinions.
ReplyGreat site, stick with it!
my site ... pirater un compte facebook
Good Tutorial for the beginners, Thank you.
Replythx a lot brother this helpe me so much i appreciate this
ReplyWelcome
Replyhow to implement it in Listview?
ReplyCreate listview with using custom adapter and then set that bitmap values into imageview thats it.
Replywhat if its a large image and the String datatype in java cant accomodate the base64 image string. is there anyway to do that???
ReplyThanks
EmoticonEmoticon