How to pass images between Activities?

Asked

Viewed 1,753 times

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 many ways and the closest thing I’ve come is this::

Passing by...

@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 stay fixed for each clicked item and do not pass the images referring to the item.

  • Does it have to be the image itself? It can’t be your reference?

  • Hi Diego Felipe, It may be the reference.Thank you so much for the attention. I’m going crazy with this guy.

  • Post the code of that onClick()

  • If I understand your doubt correctly, the images always look the same as R.drawable.slide1, right? Wouldn’t it just be changing Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.slide1); for Bitmap bmp = BitmapFactory.decodeResource(getResources(), clickedImageView.getId());? Where clickedImageView would be the View of the method onClick(View).

  • @Rubeno.Chiavone Don’t want to put an answer? I have given an alternative but it is in trouble due to the size of the images. I think your suggestion solves the problem.

  • Modesto Barreto, which Listener is the one with the callback onItemClick(View, int)? And how do you add it to RecyclerView?

Show 1 more comment

2 answers

2

What you have to do is pass the Bitmap who is in the Imageview of list item.
The same way you do for Textview get the reference to the Imageview:

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

Get the Bitmap of Imageview as follows:

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

The class Bitmap implements the interface Parcelable their objects can be passed in a Bundle:

intent.putExtra("IMAGEMJ", jovens);

To receive do:

Bitmap jovem = (Bitmap) intent.getParcelableExtra("IMAGEMJ");
ImageView image = (ImageView) rootView.findViewById(R.id.imageView1);
image.setImageBitmap(jovem);
  • Hi ramaral. I did everything right but it returns an error. When I click on the item it gives the following: E/Javabinder: !!! FAILED BINDER TRANSACTION !!!.

  • This is due to the image(bitmap) being too large. You should compress/reduce it before placing it in Imageview.

  • Where do you get these images?

  • They are in the drawable folder and before I compacted as document. But I think that’s right. I tried to compress as I did in the previous code but I couldn’t. If you find it necessary I can put here as I did. Again Thank you.

  • Each has 50 to 90 k

  • See the comment @Rubeno.Chiavone left in the question.

  • I had already seen and tried but it did not work. My list has no ID. I tried to put but it did not work. Anyway thank you very much.

Show 2 more comments

0

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

Passing by...

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);
  • Hi Victor, My problem is just this. I don’t know how to change dynamically. Of course I know which image is associated with the item. Thanks for the interest.

  • At some point you had to build an Adapter to feed Recyclerview. In this adapater you certainly passed him a List<> or an Arraylist<> of a certain object created by you for this Recyclerview. The only way your Recyclerview will display images is if each List<> or Arraylist<> object you passed to the Adapter contains a reference to the image R.drawable.filename. This way you can use the position value that the onItemClick() method returns you to access the object of your List<>/Arraylist<> and get that image.

  • Hi Victor, I’m sorry but I was on a business trip and I just saw your suggestion today. It worked for the first image, but only passes until the 5th, from the #6 onwards it closes the application. Also when I add the second image it closes. Maybe it’s because my list is too long? I have 179 items.

  • Can you show me what the error is? And by the way it is useful if you show me the part of the code where the error occurs.

  • Hi Victor, He did not give message, just closed.

  • O código tinha ficado assim: Bitmap bmpj = BitmapFactory.decodeResource(getResources(), mEspecie.get(position).getPhotoIDj());&#xA;ByteArrayOutputStream stream = new ByteArrayOutputStream();&#xA;bmpj.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] young = stream.toByteArray(); What I did and worked was this: I changed 100 to 50 and gave a fixed size to the 2 Imageview 330 by 250 which was wrap_content

  • already took a look at the android monitor to see if nn gave out of memory?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.