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

}

The above is the MyAdapter method. I want to pass the value of the bitmap to another activity in order to display the onItemClicked image from the RecyclerView list into another activity. My another question is that do I need to pass the intent stuff in my MainActivity's class method in

@override 
Public void itemClickedmethod(View view,int position)    {  
    Bundle extras=new bundle(); 

and so. .the intent stuff. or somewhere else. Please help.

Bhargav
  • 8,118
  • 6
  • 40
  • 63
Sayyaf
  • 316
  • 1
  • 4
  • 12
  • You want to pass the bitmap thats in the `view`? – Bhargav Nov 06 '15 at 07:04
  • i have the bitmap in Async task in doInBackground method. when i click the image in the reyclerview it should go to next activity and display the image. but i need to pass the bitmap right so that i can display the image in next activity. how to get that bitmap so that i can pass via Intent. – Sayyaf Nov 06 '15 at 07:25

2 Answers2

0

Bitmap implements Parcelable, so you could always pass it in the intent:

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

and retrieve it on the other end:

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

Taken from the answer given here

EDIT 1

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

use that to get the bitmap attached to an ImageView. (image is the imageview)

Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • That's ok Bharghav but,Im getting the URL from the MainActivity Class. I'm able to display 100 images in a Recycler List view. Now when I click one Particular image it should open in next activity. But i dont know how to get the bitmap from MyAdapter class which is present in Async task-> doInBackground method. so when i intent to another activity. I must recieve that bitmap in another activity so that i can set the bitmap to into image view. So that i would able to display the image in next activity. – Sayyaf Nov 06 '15 at 11:16
  • @Sayyaf `Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();` use that to get the bitmap attached to an imageview – Bhargav Nov 06 '15 at 11:18
  • while doing intent, we generally pass Bundle stuff like data right? so here now since i have to pass an image im asking whether i have to put the bitmap in the intent? Since i'm new to programming im facing issues bare with me – Sayyaf Nov 06 '15 at 11:24
  • Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); this i would use in secondActivity class right? My question is do i need to pass the bitmap in the intent? – Sayyaf Nov 06 '15 at 11:26
  • yes you can also pass objects that are parcelable in your intent and bitmap id parcelable – Bhargav Nov 06 '15 at 11:26
  • no you get the bitmap from the imageview in your adapter using the code i just mentioned in the comment and pass that bitmap in the intent's extras to your next activity where you want to show the image – Bhargav Nov 06 '15 at 11:28
  • if you please see my program here public void setClickListener(ClickListener clickListener) { this.clickListener=clickListener; Bundle extras=new Bundle(); extras.putParcelable("image",bitmap); } here Bundle extras=new Bundle(); this i have done wrong i know. I was just trying. its generally declared in mainActivity where I've declared public void itemClicked(View view,int position){ //intent functions done here.. it is present in MainActivity }. Instead of declaring there I've declared in setListener method. – Sayyaf Nov 06 '15 at 11:31
  • As you have said even if i get the bitmap but where should I get i mean in which function? and i'm doing the Intent in MainActivity so will it be accessable from there to send the bitmap? please can u write an sample code for that pls? – Sayyaf Nov 06 '15 at 11:35
  • You are passing the imageview in `itemClicked` yes? and implementing ClickListener in your mainactivity right? so in the body of itemClicked you just get the bitmap from the view and pass it to the intent in your acitivty – Bhargav Nov 06 '15 at 11:38
0

try with this code this code is working :-

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

// then

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

        }
    }
}
Amitsharma
  • 1,577
  • 1
  • 17
  • 29
  • Im getting the URL from the MainActivity Class. I'm able to display 100 images in a Recycler List view. Now when I click one Particular image it should open in next activity. But i dont know how to get the bitmap from MyAdapter class which is present in Async task-> doInBackground method. so when i intent to another activity. I must recieve that bitmap in another activity so that i can set the bitmap to into image view. So that i would able to display the image in next activity. – Sayyaf Nov 06 '15 at 11:15
  • @Sayyaf you need to get that data in ArrayList and at set this in list then make a adapter class then get position of list adapter to get particular image .... – Amitsharma Nov 09 '15 at 08:24
  • my code will display if image on server then you can display that image in to your image view ..... you have to manage your data in your coding management ..... – Amitsharma Nov 09 '15 at 08:25