How to move images between Activities?

2

I have a list (RecyclerView) with two texts and two images in each item. In onClick I can pass the texts but not the images. I've tried it in several ways and what I got closer was with the following:

Passing ...

@Override
public void onItemClick(View view, int position) {

    TextView txt = (TextView) view.findViewById(R.id.nome_cientifico);  
    String str = txt.getText().toString();

    TextView txt1 = (TextView) view.findViewById(R.id.nome_comum);  
    String str1 = txt1.getText().toString();

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.slide1);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] jovens = stream.toByteArray();

    Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.slide3);
    ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
    bmp1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
    byte[] adultas = stream1.toByteArray();

    Intent intent=new Intent(getActivity(), Detalhes.class);

    intent.putExtra("CIENTIFICO", str);

    intent.putExtra("COMUM", str1);

    intent.putExtra("IMAGEMJ", jovens);

    intent.putExtra("IMAGEMA", adultas);

    startActivity(intent);

}

Receiving ....

Intent intent = getActivity().getIntent();
Bundle extras = getActivity().getIntent().getExtras();
View rootView = inflater.inflate(R.layout.fragment_detalhes, container, false);

if (intent != null && intent.hasExtra("CIENTIFICO")) {

    mMato = intent.getStringExtra("CIENTIFICO");
    ((TextView) rootView.findViewById(R.id.textView1)).setText(mMato);

    mMato1 = intent.getStringExtra("COMUM");
    ((TextView) rootView.findViewById(R.id.textView2)).setText(mMato1);

    byte[] byteArray = extras.getByteArray("IMAGEMJ");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView image = (ImageView) rootView.findViewById(R.id.imageView1);
    image.setImageBitmap(bmp);

    byte[] byteArray1 = extras.getByteArray("IMAGEMA");
    Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray1, 0, byteArray1.length);
    ImageView image1 = (ImageView) rootView.findViewById(R.id.imageView2);
    image1.setImageBitmap(bmp1);
}

My problem is with the image R.drawable.slide1 and R.drawable.slide3 Passing so they are fixed for each item clicked and does not pass the images referring to the item.

    
asked by anonymous 26.01.2016 / 20:48

2 answers

2

What you have to do is pass the Bitmap that is in the ImageView of the list item. Just as it does for TextView get the reference to ImageView :

ImgeView slide1 = (ImageView) view.findViewById(R.id.slide1);//Altere para o R.id correcto

Get the Bitmap from ImageView as follows:

Bitmap jovens =((BitmapDrawable)slide1.getDrawable()).getBitmap();

The class Bitmap implements the Parcelable where your objects can be passed in a Bundle :

intent.putExtra("IMAGEMJ", jovens);

To get done:

Bitmap jovem = (Bitmap) intent.getParcelableExtra("IMAGEMJ");
ImageView image = (ImageView) rootView.findViewById(R.id.imageView1);
image.setImageBitmap(jovem);
    
27.01.2016 / 20:38
0

If you know which image is associated with the item you click then you can do this:

Passing ...

Intent intent=new Intent(getActivity(), Detalhes.class);
int imageId = R.drawable.slide1; // Aqui tens de mudar dinamicamente (de acordo com o item que clicas) para a imagem que pretendes passar para os detalhes
intent.putExtra("IMAGEMJ", imageId );

startActivity(intent);

Receiving ....

int imageId= extras.getInt("IMAGEMJ");
Bitmap bmp = BitmapFactory.decodeResource(getResources(), imageId);
ImageView image = (ImageView) rootView.findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
    
27.01.2016 / 15:45