gonga dev
gonga dev

Reputation: 117

How to send image from one activity to another in android

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.

Upvotes: 2

Views: 5918

Answers (5)

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

Upvotes: 0

Sam
Sam

Reputation: 4284

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

Upvotes: 0

Sofiane Daoud
Sofiane Daoud

Reputation: 878

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

Upvotes: 1

Newbiee
Newbiee

Reputation: 592

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.

Upvotes: 1

Raziel25
Raziel25

Reputation: 405

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.

Upvotes: 6

Related Questions