如何从listview获取图像?

时间:2013-06-07 13:23:21

标签: android listview bitmap nullpointerexception

我想将图像从列表视图发送到其他活动。我想我已经完成了大部分工作,但我一直在努力获得nullpointerexeptioon。似乎我的位图是null。如何将选择项目中的图像作为资源转换为位图?

lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long id) {

                    ImageView imm_id = ((ImageView) arg1.findViewById(R.id.imagePrev));
                    imm_id.getId();
                    RequestDataHolder.request_id = ids.get(position);
                    Bitmap bitmap=BitmapFactory.decodeResource(getResources(), imm_id.getId());
                    ByteArrayOutputStream stream=new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
                    byte[] imm=stream.toByteArray();
                    Intent i = new Intent(getApplicationContext(),ResultViewActivity.class);
                    i.putExtra("imMap", imm);
                    startActivity(i);

适配器

public class MediaItemAdapter extends SimpleAdapter {
    private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
    public static boolean notifyFlag = false;
    private Context localContext;
    private List<HashMap<String, Object>> locallist;
    private static LayoutInflater inflater=null;
    private Object rslv;
    public ImageView imgV;
    public String valueV;
    Map cache = new HashMap();
    public MediaItemAdapter(Context context,
            List<HashMap<String, Object>> items, int resource,
            String[] from,
            int[] to) {
        super(context, items, resource, from, to);
        localContext = context;
        locallist = items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public void setViewText(TextView v, String text) {
        // метод супер-класса, который вставляет текст
        super.setViewText(v, text);
        {
            v.setText(Html.fromHtml(text));
        }
    }
    @Override
    public void setViewImage(ImageView v, String value) {
        imgV = v;
        valueV = value;
        super.setViewImage(v, value);
        if (imgV.getId() == R.id.imagePrev) {
            if (value == null || value.trim().length() <= 0
                    || value.equalsIgnoreCase("null")
                    || value.trim().equalsIgnoreCase("")) {
                imgV.setImageResource(R.drawable.stub);
                // do nothing
            } else {
                if (cache.get(value) == null
                        && !SearchPostActivity.loadingMore) {
                    notifyFlag = false;
                    new LazyImageLoader(localContext, value).execute();
                } else {
                    imgV.setImageBitmap((Bitmap) cache.get(value));
                }
            }
        }
    }

    class LazyImageLoader extends AsyncTask<String, Integer, Bitmap> {
        private String URL;
        private Context context;

        public LazyImageLoader(Context context, String Url) {
            URL = Url;
            LazyImageLoader.this.context = context;
        }
        @Override
        protected void onPreExecute() {
            imgV.setImageResource(R.drawable.home);
        }
        @Override
        protected Bitmap doInBackground(String... params) {
            return getImg();
        }
        @Override
        protected void onPostExecute(Bitmap img) {
            if (!SearchPostActivity.scrollFlag) {
                imgV.setImageBitmap(img);
                notifyDataSetChanged();
            }
            notifyFlag = true;
            cache.put(URL, img);
        }
        public synchronized Bitmap getImg() {
            Bitmap bm = null;
            try {
                URL myurl;
                myurl = new URL(URL);
                URLConnection conn = myurl.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                bm = BitmapFactory.decodeStream(is);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bm;

        }

    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item_composer, null);
        ImageView thumb_image=(ImageView)vi.findViewById( R.id.imagePrev);

        return vi;
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个

 yourListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long id) {
        final ImageView imageView = (ImageView) view.findViewById(R.id.imageViewXYZ);
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
        final Bitmap yourBitmap = bitmapDrawable.getBitmap();
    }
});

答案 1 :(得分:0)

最好使用调试工具在

找出getResources()imm_id.getId()的值
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),imm_id.getId());