从Base64字符串预览ImageView中的BitMap

时间:2016-06-13 06:23:24

标签: android

我尝试将BitMap作为Base64字符串存储在数据库中,但不知怎的,我只是无法在ImageView中预览它。这是代码:

商店数据:

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

检索数据:

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 (我的客户ListView适配器)

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

我打印此代码的结果:List list = db.getList(params);,其中有数据,但问题是:图片不会显示在ImageView

1 个答案:

答案 0 :(得分:1)

问题是您将图片编码为$sql="DELETE expense, invoice, cat, conf, patient, prv, test_invoice, test_style FROM expense, invoice, cat, conf, patient, prv, test_invoice, test_style"; Base64,但您实际上从未对其进行解码。

解码:

String

实际流程应该是这样的:

String encodedBytes = params.get("image"); byte[] decodedBytes = Base64.decode(encodedBytes, Base64.DEFAULT); Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); - &gt; Bitmap数组 - &gt; byte编码Base64 - &gt;到DB。

来自DB - &gt; String编码Base64 - &gt;解码String数组 - &gt;解码byte