欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-20 21:02:04
...
 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<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
            String photo = null;
            String ret= null;

            for(int i=0;i<photoList.size();i++) {
                photo = String.valueOf(photoList.get(i));
                String regex="photoInfo='(.*?)'";
                Matcher matcher= Pattern.compile(regex).matcher(photo);
                while(matcher.find()) {
                    ret=matcher.group(1);
                }

                Map<String,Object> map = new HashMap<String, Object>();
                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);

        }
    }