Ethyl Casin Ethyl Casin - 5 months ago 39
Android Question

Preview BitMap in ImageView from a Base64 string

I tried to store a

BitMap
in the database as
Base64
string, but somehow I just cant preview it in the
ImageView
. 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();
db.create(params);


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());
image_list.setLayoutParams(layout);


ImageItem.java:

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); }

}


DbBitmapUtility.java

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);
}

}


ImageItemAdapter.java (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;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int pos) {
return pos;
}

@Override
public long getItemId(int pos) {
return pos;
}

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

ImageView image = (ImageView) rowView.findViewById(R.id.image_menu_view);
TextView title = (TextView) rowView.findViewById(R.id.image_menu_text);

ImageItem item = data.get(pos);
if(item != null){
image.setImageBitmap(item.getImage());
title.setText(item.getTitle());
}

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
ImageView
.

Answer

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.

Comments