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");
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 to put image in a bundle and pass it to another activity
So you can do it like this, but the limitation with the Parcelables is that the payload between activities has to be less than 1MB total. It's usually better to save the Bitmap to a file and pass the URI to the image to the next activity.
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.my_layout);
Bitmap bitmap = getIntent().getParcelableExtra("image");
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);
}
How to send the bitmap into bundle
First of all convert it to a Byte array
before adding it to intent, send it out, and decode.
//Convertion to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bundle b = new Bundle();
b.putByteArray("image",byteArray);
// your fragment code
fragment.setArguments(b);
get Value via intent
byte[] byteArray = getArgument().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
How to share and save the ImageView from First Activity to Third Activity
I recommend using File I/O to save the bitmap to disk. Put the path of the file into your intent bundle using putExtra and recreate the bitmap in your other screens. Putting large bitmaps into a bundle with an intent can cause TransactionTooLarge exceptions.
Take a look at Getting path of captured image in android using camera intent
How to pass image data from one activity to another activity?
In MyGridView: (someInteger is an integer that represents the index of the selected image
Intent myIntent = new Intent(this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("image", someInteger);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
In MyImageViewActivity:
Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("image");
of course, you can put anything in the bundle! maybe a byte array or something
Related Topics
Android Broadcastreceiver on Startup - Keep Running When Activity Is in Background
Is the Way the Firebase Database Quickstart Handles Counts Secure
How to Listen For a Webview Finishing Loading a Url
Application Skeleton to Support Multiple Screens
Adding External Library in Android Studio
Firebase Data Desc Sorting in Android
How to Send a Json Object Over Request With Android
How to Create an Object of an Activity in Other Class
Install/Uninstall Apks Programmatically (Packagemanager VS Intents)
How to Check the Current Status of the Gps Receiver
Android Gradle Plugin 0.7.0: "Duplicate Files During Packaging of Apk"
How to Implement My Very Own Uri Scheme on Android
How to Create a Release Signed Apk File Using Gradle
How to Add the New "Floating Action Button" Between Two Widgets/Layouts
Height of Status Bar in Android
Android: Show Soft Keyboard Automatically When Focus Is on an Edittext