2

Main Activity

holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       String title = ((TextView) view.findViewById(R.id.recipe_title)).getText().toString();
       String time = ((TextView) view.findViewById(R.id.time)).getText().toString();
       String servings = ((TextView) view.findViewById(R.id.servings)).getText().toString();
       String calories = ((TextView) view.findViewById(R.id.calories)).getText().toString();
       ImageView thumbnail = (ImageView) view.findViewById(R.id.recipe_img);
       Intent intent = new Intent(context, ActivityDetail.class);
       intent.putExtra("RecipeTitle", title);
       intent.putExtra("RecipeTotalTime", time);
       intent.putExtra("RecipeServings", servings);
       intent.putExtra("RecipeCalories", calories);
       context.startActivity(intent);
    }
});

Detail Activity

TextView time = (TextView)findViewById(R.id.time_detail);
TextView servings = (TextView)findViewById(R.id.servings_detail);
TextView calories = (TextView)findViewById(R.id.calories_detail);
ImageView thumbnail = (ImageView)findViewById(R.id.image_paralax);
time.setText(getIntent().getExtras().getString("RecipeTotalTime"));
servings.setText(getIntent().getExtras().getString("RecipeServings"));
calories.setText(getIntent().getExtras().getString("RecipeCalories"));

How can I send a thumbnail from the first Activity to second Activity. In the second `Activity, the thumbnail will be shown on an ImageView.

Jeff
  • 12,555
  • 5
  • 33
  • 60
gonga dev
  • 117
  • 3
  • 13
  • 1
    Possible duplicate of [how to pass images through intent?](http://stackoverflow.com/questions/23577827/how-to-pass-images-through-intent) – Mr.7 Jul 01 '16 at 16:10

5 Answers5

6

You should not try to send a image through a Intent. Rather it is better to send the Uri of the image.. And in the second activity load the image from the Uri.

Raziel25
  • 405
  • 1
  • 4
  • 13
  • It depends from how your are loading your image (i. e. Path, File) if you are using a File you can get it's Uri like this. Uri retrivedUri = Uri.fromFile(imageFile); then put it in the intent as a Parcelable. myIntent.putParcelable(”yourKey", retrivedUri); – Raziel25 Jul 01 '16 at 16:33
1

I dont recommend you to pass bitmap to other activity, instead you should pass the string url and in the receiving activity you can load image from web or file.

if you really want to pass image using bitmap only then you have to Convert it to a Byte array before you add it to the intent, send it out, and decode.

Bitmap bmp = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);

Then in your receiving activity you will need to add following code to receive the byte array:

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

now you have a bitmap you can add setImageDrawable(new BitmapDrawable(mContext.getResources(), bmp));

Thats all you need to do.

Newbiee
  • 592
  • 6
  • 22
1

It is not a good thing to send and image into an intent, you should try to send the uri of the image instead.

But if you really want to do it, so you can get the Bitmap drawable from the image view:

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

Since the bitmap is a Parcelable you can send it directly as an Extra:

intent.putExtra("RecipeThumbnail", bitmap); 
Sofiane Daoud
  • 868
  • 9
  • 21
0

Yes, it is possible.. but not recommended

1)First you have to make bitmap and bytes array from that bitmap

Bitmap bitmap = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();

2) then you can pass it using intent

intent.putExtra("Thumbnail Image", image);

3) In receiving activity,

byte[] = getIntent.getExtra("Thumbnail Image");
Bitmap bmp = BitmapFactory.decodeByteArray(byte, 0, byteArray.length);
imageView.setImageBitmap(bmp);
Sam
  • 4,046
  • 8
  • 31
  • 47
0

Do it faster like a master :)

pass the id of the image, you have it in your resources, so why seralisin so much info if the activity b can just loading it by knowing the id...

Intent intent = new Intent(context, ActivityDetail.class);
...
intent.putExtra("imageId", R.id.recipe_img);

and on the other activity will be enough doing:

ImageView thumbnail = (ImageView) view.findViewById(yourREceivedImageId);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97