0

i'm trying to send an image from an arraylist one activity to another through intent in recyclerView. But in putextra method it shows error like cannot resolve method 'putextra(java.lang.string,android.widget.imageview)'

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageView img = images.get(position);
            Intent intent = new Intent(context,Result.class);
            intent.putExtra("Image",img);
            context.startActivity(intent);
        }
    });
Awais Aslam
  • 75
  • 1
  • 1
  • 9
  • we can't pass any view using intent , if you want to send image then you can send image uri, file , or url using intent and get it in second activity – Android Geek Aug 01 '19 at 09:52

3 Answers3

0

You cannot pass an ImageViewas an extra, the object you are passing must be a Parcelable or a Serialiazable.

Your ImageView is also relevant for this activity only. If you are trying to send an Image to another activity, it is better to send the path to the image.

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
0

You cannot send image directly because intent does not support it

Try this Solution

You can send image using ByteArray though eg

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageView img = images.get(position);
            img.buildDrawingCache();
            Bitmap bmp = ((BitmapDrawable)img.getDrawable()).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            Intent intent = new Intent(this, Result.class);
            intent.putExtra("Image", byteArray);
            context.startActivity(intent);
        }
    });

in onCreate() of Result Activity class

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("Image");
//convert it to bitmap again
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
//set image to bitmap
image.setImageBitmap(bmp);

SOURCE Passing image from one activity another activity

Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

At the end of the day finally i did it, you simply create an array of images(NOTE: don't create arrayList) in case u want to choose between multiple images and simply pass these images through an integer. below is the code...

public void onBindViewHolder(MyViewHolder holder, final int position) {

    holder.image.setImageResource(images[position]);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int img = images[position];

            Intent intent = new Intent(context,Result.class);
            intent.putExtra("Images", img);
            intent.putExtra("personName", Name);
            intent.putExtra("Gender", Gender);
            context.startActivity(intent);
        }
    });
}

//In targeted activity

public void getdata(){

    if (getIntent().getExtras() !=null) {

        gender = getIntent().getStringExtra("Gender");
        Name = getIntent().getStringExtra("personName");
        int image = getIntent().getIntExtra("Images",0);
        mytext(gender,Name,image);
    }
}
public void mytext(String gender,String Name,int img){
    textView1 = findViewById(R.id.txt1);
    textView2 =findViewById(R.id.txt2);
    imageView = findViewById(R.id.Imagename);
    textView1.setText(Name);
    textView2.setText(gender);
    imageView.setImageResource(img);
}

//then in onCreate method call "getdata();"

Awais Aslam
  • 75
  • 1
  • 1
  • 9