Ethyl Casin Ethyl Casin - 1 year ago 146
Android Question

Preview BitMap in ImageView from a Base64 string

I tried to store a

in the database as
string, but somehow I just cant preview it in the
. Here's the code:

Store Data:

Map params = new HashMap();
params.put("faasid", faasid);
params.put("title", data_title);
params.put("image", Base64.encodeToString(DbBitmapUtility.getBytes(bitmap),Base64.DEFAULT));

ImageDB db = new ImageDB();

Retrieve Data:

List<ImageItem> data = new ArrayList<ImageItem>();

Map params = new HashMap();
params.put("faasid", faasid);

ImageDB db = new ImageDB();
List<Map> list = db.getList(params);

for(Map m : list){
String title = m.get("title") != null ? m.get("title").toString() : "";
String image = m.get("image") != null ? m.get("image").toString() : "";

data.add(new ImageItem(faasid, title, image.getBytes()));

image_list.setAdapter(new ImageItemAdapter(activity,data));
LayoutParams layout = (LayoutParams) image_list.getLayoutParams();
layout.height = (320 * data.size());

public class ImageItem {

private String faasid, title;
private byte[] image;

public ImageItem(String faasid, String title, byte[] image){
this.faasid = faasid;
this.title = title;
this.image = image;

public String getFaasId(){ return faasid; }
public String getTitle(){ return title; }
public Bitmap getImage(){ return DbBitmapUtility.getImage(image); }


public class DbBitmapUtility {

// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 80, stream);
return stream.toByteArray();

// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);

} (my custome ListView adapter)

public class ImageItemAdapter extends BaseAdapter{

LayoutInflater inflater = null;
Context ctx;
List<ImageItem> data;

public ImageItemAdapter(Activity activity, List<ImageItem> data){
ctx = activity; = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

public int getCount() {
return data.size();

public Object getItem(int pos) {
return pos;

public long getItemId(int pos) {
return pos;

public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.image_menu, null);

ImageView image = (ImageView) rowView.findViewById(;
TextView title = (TextView) rowView.findViewById(;

ImageItem item = data.get(pos);
if(item != null){

return rowView;

public ImageItem getListItem(int pos){
return data.get(pos);

I print the result of this code:
List list = db.getList(params);
, and there was data in it, but the problem is: The image wont appear in the

Answer Source

The problem is you encode your image into a Base64 String, but you never actually decode it.

For the decoding:

String encodedBytes = params.get("image");
byte[] decodedBytes = Base64.decode(encodedBytes, Base64.DEFAULT);
Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

The actual flow should be something like this:

Bitmap -> byte array -> Base64 encoded String -> To DB.

From DB -> Base64 encoded String -> decoded byte array -> decoded Bitmap.