如何获取Asynctask中存在的onItemclicked方法中的位图,以将位图包传递给另一个活动?

时间:2015-11-06 06:57:38

标签: java android bitmap android-asynctask

    @Override
    protected Bitmap doInBackground(String... imageurl)
    {
            try {
                URL url = new URL(imageurl[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                InputStream in = urlConnection.getInputStream();
                bitmap = BitmapFactory.decodeStream(in);

                return bitmap;
            }catch (Exception e)
            {

                e.printStackTrace();
                return null;
            }
        }

    @Override
    protected void onPostExecute(Bitmap result) {

        super.onPostExecute(result);
        if(result!=null && imageReference!=null) {
            ImageView imgview = (ImageView) imageReference.get();
            if (imgview != null) {
                imgview.setImageBitmap(bitmap);
            }
        }

    }
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    ItemData current=data.get(position);
    try {
        Mytask mytask=new Mytask(holder.getImageview());
        mytask.execute(current.imageUrl);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
public void setClickListener(ClickListener clickListener)
{
    this.clickListener=clickListener;
    Bundle extras=new Bundle();
    extras.putParcelable("image",bitmap);


}


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

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView icon;
    public MyViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        icon=(ImageView)itemView.findViewById(R.id.item_icon);
    }
   public ImageView getImageview()
   {
       return icon;
   }
    public void onClick(View view)
    {
        if(clickListener!=null)
        {
            clickListener.itemClicked(view,getAdapterPosition());
        }
    }

}

public interface ClickListener{
    public void itemClicked(View view,int position);

}

以上是MyAdapter方法。我想将位图的值传递给另一个活动,以便将来自RecyclerView列表的onItemClicked图像显示到另一个活动中。我的另一个问题是,我需要在

中的MainActivity类方法中传递intent内容
@override 
Public void itemClickedmethod(View view,int position)    {  
    Bundle extras=new bundle(); 

等等。意图的东西。或者别的地方。请帮忙。

2 个答案:

答案 0 :(得分:0)

Bitmap实现Parcelable,因此您始终可以在意图中传递它:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

并在另一端检索它:

Intent intent = getIntent();
   Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

取自给出的答案here

编辑1

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

使用它来将位图附加到ImageView。 (image是imageview)

答案 1 :(得分:0)

尝试使用此代码,此代码正常运行: -

   imageUrl = "nameofimagefile or path of image at server side"
   new LoadImage().execute(imageUrl);

//然后

private class LoadImage extends AsyncTask<String, String, Bitmap> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();



        //pDialog = new ProgressDialog(getActivity());
        //pDialog.setMessage("Loading Image ....");
        //pDialog.show();
    }

    protected Bitmap doInBackground(String... args) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                    args[0]).getContent());

            System.out.println("======" + bitmap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap image) {

        if (image != null) {

            imgCompLogo.setImageBitmap(image);
            //pDialog.dismiss();

        } else {
            //pDialog.dismiss();

        }
    }
}