Android Base64转换图片,SimpleAdapter动态显示Bitmap图片

 public Bitmap stringtoBitmap(String string){
        //将字符串转换成Bitmap类型
        Bitmap bitmap=null;
        try {
            byte[]bitmapArray;
            bitmapArray=Base64.decode(string, Base64.DEFAULT);
            bitmap= BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }

SimpleAdapter 默认是静态显示程序内部的本地资源;在显示网上的远程图片时,需要实现ViewBinder()接口,在里面定义数据和视图的匹配关系。

public void Search(){
         
            List> data = new ArrayList>();
            String photo = null;
            String ret= null;

            for(int i=0;i map = new HashMap();
                map.put("imageView1",stringtoBitmap(ret));
                data.add(map);
            }

            SimpleAdapter simplead = new SimpleAdapter(
                    mContext,
                    data,
                    R.layout.photo_history_item,
                    new String[] { "imageView1"},
                    new int[] {R.id.imageView1});
            //实现ViewBinder()这个接口
            simplead.setViewBinder(new SimpleAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View view, Object data,
                                            String textRepresentation) {
                    if(view instanceof ImageView && data instanceof Bitmap){
                        ImageView i = (ImageView)view;
                        i.setImageBitmap((Bitmap) data);
                        return true;
                    }
                    return false;
                }
            });
            GridView1.setAdapter(simplead);

        }
    }


你可能感兴趣的:(Android)