Passing Bitmap between two activities [duplicate]
Solution 1:
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);
}
Solution 2:
I think that for most cases, it's ok to put the bitmap on a static reference that should be null-ed when the receiving activity doesn't need it anymore.
This way, it's very fast and doesn't need to be converted or encoded&decoded. It also doesn't leave traces behind.
Solution 3:
There are lots of ways :
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");
2)
Convert it to a Byte array before you add it to the intent, send it out, and decode.
//Convert to byte array
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 Activity 2:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Solution 4:
I have answered a similar question in this SO post. My solution will save the image in the internal storage of the device inaccessible by other apps(on unrooted phones). Then we can simply decode the file and show it in imageview. It prevents TranscationTooLarge exception as you cannot exceed 1mb limit of passing bundle.