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

You can do with these tricks

Trick 1 :
 
Bitmap implements Parcelable, so you could always pass it in 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");


 Trick 2 :

Intent i = new Intent(this, Second.class)
i.putExtra("Image", bitmap);
startActivity(i)
And, in Second.class
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Image");
 
 

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

in your first activity - 

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 next activity -

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



No comments:

Post a Comment