Passing android Bitmap Data within activity using Intent in Android
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);
edit
Thought I should update this with best practice:
In your first activity, you should save the Bitmap to disk then load it up in the next activity. Make sure to recycle your bitmap in the first activity to prime it for garbage collection:
Activity 1:
try {
//Write file
String filename = "bitmap.png";
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bmp.recycle();
//Pop intent
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image", filename);
startActivity(in1);
} catch (Exception e) {
e.printStackTrace();
}
In Activity 2, load up the bitmap:
Bitmap bmp = null;
String filename = getIntent().getStringExtra("image");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
Cheers!
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);
}
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");
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.
Related Topics
Call an Activity Method from a Fragment
Android: Go Back to Previous Activity
Update Data in Listfragment as Part of Viewpager
How to Validate an E-Mail Address
How to Display Count of Notifications in App Launcher Icon
Vertical (Rotated) Label in Android
Environment.Getexternalstoragedirectory() Deprecated in API Level 29 Java
How to Do Something Like a Flowlayout in Android
How to Start a Service When .Apk Is Installed For the First Time
Removing an Activity from the History Stack
Android Take Screenshot of Surface View Shows Black Screen
Difference Between a View'S Padding and Margin
Android: Alternate Layout Xml For Landscape Mode
Android - Android.Os.Networkonmainthreadexception
"Conversion to Dalvik Format Failed With Error 1" on External Jar
How to Support Different Screen Size in Android