Passing Bitmap Between Two Activities

Passing Bitmap between two activities

You can simply try with below -

Intent i = new Intent(this, Second.class)
i.putExtra("Image", bitmap);
startActivity(i)

And, in Second.class

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Image");

Have a look at here If you want to compress your Bitmap before sending to next activity just have a look at below -

Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);

in your nextactivity -

if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent()
.getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(b);
}

transfer bitmap between two activities in Kotlin

You need to pass the bitmap as an extra argument to the intent while starting the activity.

val intent = new Intent(this, NewActivity::class.java)
intent.putExtra("BitmapImage", bitmap)
startActivity(intent);

and retrieve it as:

val bitmap = this.intent?.getParcelableExtra("BitmapImage") as Bitmap

I simply translated the code Here to kotlin. You should use Android Studio to translate Java code to Kotlin.

how do you pass images (bitmaps) between android activities using bundles?

I would highly recommend a different approach.

It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example intent.putExtra("data", bitmap). A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable.

If you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.

How can I pass a Bitmap object from one activity to another

Bitmap implements Parcelable, so you could always pass it with the intent:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and retrieve it on the other end:

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Pass a Bitmap between 2 activities

You'd rather pass image URI instead of image data.

In the onActivityResult() method you can get the image URI:

Uri imageUri = data.getData();

Then you can pass this uri to the other activity when starting it:

    Intent intent = new Intent(context, MyNextActivity.class);
intent.setData(imageUri);
startActivity(intent);

And in the next activity you can retrieve this URI:

Uri imageUri = getIntent().getData();

And then to display the image in an ImageView:

imageView.setImageURI(imageUri);


Related Topics



Leave a reply



Submit