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

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


Actually, passing a bitmap as a Parcelable will result in a "JAVA BINDER FAILURE" error. Try passing the bitmap as a byte array and building it for display in the next activity.

I shared my solution here:
how do you pass images (bitmaps) between android activities using bundles?


Passsing bitmap as parceable in bundle between activity is not a good idea because of size limitation of Parceable(1mb). You can store the bitmap in a file in internal storage and retrieve the stored bitmap in several activities. Here's some sample code.

To store bitmap in a file myImage in internal storage:

public String createImageFromBitmap(Bitmap bitmap) {    String fileName = "myImage";//no .png or .jpg needed    try {        ByteArrayOutputStream bytes = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);        FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);        fo.write(bytes.toByteArray());        // remember close file output        fo.close();    } catch (Exception e) {        e.printStackTrace();        fileName = null;    }    return fileName;}

Then in the next activity you can decode this file myImage to a bitmap using following code:

//here context can be anything like getActivity() for fragment, this or MainActivity.thisBitmap bitmap = BitmapFactory.decodeStream(context.openFileInput("myImage"));

Note A lot of checking for null and scaling bitmap's is ommited.