In Activity A, I download an image using Picasso and save into a Bitmap. I need that Bitmap in Activity B, how can I send it in the bundle without saving the image into the file system, and without sending the bitmap, as it's not very efficient?
EDIT: Is it possible to use the resource ID? If so, how?
If you strictly don't want to pass Bitmap to Bundle, you can extend Application class, and store/get bitmap to/from it. However, I'm asking you not to do this, and consider to send Bitmaps through Bundles, because storing such data in Application class is much worse then overhead from sending or storing in file system.
Bitmap implements Parcelable interface. So, you can pass it via bundle.
For sending,
Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
intent.putExtra("bitmap", bitmap);
startActivity(intent);
To retrieve it,
Bitmap bitMap = getIntent().getParcelableExtra("bitmap");
Related
I'm developing Web-client on Android. I use IntentService for http-request. As a result, the object are formed by IntentService which have not only the primitive types, but also, for example, Bitmap object field. Tell me, please, best way to pass the object in the Activity, or some another class.
I try used ResultReceiver, but callback method get only Bundle-object:
void onReceiveResult (int resultCode, Bundle resultData)
Bundle-object is only suitable for storing simple types. Translate large Bitmap-object as byte array is not recommended.
The second path, that to pack the object in the Parcel and set in Intent. Then catch it using BroadcastReceiver. How about sending large object in Broadcast message?
Perhaps you can just save the image and pass through the OutputStream way? Or just save it as a static variable somewhere? I'm not an expert of Android, the main thing I want to get fast app. Thanks for your answers!
Bundle-object is only suitable for storing simple types. Translate large Bitmap-object as byte array is not recommended.
Yes, you should never transfer large data via Intent. It's not about data type, it's about size.
The second path, that to pack the object in the Parcel and set in Intent. Then catch it using BroadcastReceiver. How about sending large object in Broadcast message?
It's same thing, sending a message still requires Intent which has limitations.
Or just save it as a static variable somewhere?
This will work but this is a bad architecture. If you do this you will always have to think about that static field and be sure at all times that you are not keeping a static reference to the Bitmap you no longer need. I strongly don't recommend this.
Instead, you can store your Bitmap object to disk and then read it in your Activity. Here is an example: Save bitmap to location
I want to know how long data can pass though intent. If i am passing parcelabel(as i am passing bitmap) more than 500x500 size it is give java transection binding fail.
If it's small (thumbnail) then it should be fine, if it is big you can end up with failed binder transaction errors. To avoid it you should pass a URI to location where it is saved. You can put it on sdcard if you have permission or in cache folder if not. If you are sending intent to some other process, then you should put bitmap on sdcard to make it readable by other processes. Last solution is to write Content Provider, this is usefull when you are sharing bitmap files, and want to for example attach bitmap with email.
I'm trying to just keep a Bitmap in memory for the next activity to use without putting it in the extras bundle due to its size or saving it to the sd card for performance issues. Is there any way to pass a reference to my Bitmap to another activity.
I want to do something like this
Bitmap myBitmap = null;
Intent newIntent = new Intent(thisClass.this, nextActivity.class);
newIntent.putExtra("bitmap", reference to myBitmap);
If I pass the full bitmap it crashes because the file is over 1MB in size, so I just want to pass a reference or possibly contain the file in another class and serialize it or something I'm not sure what to do.
You can put the bitmap in the Application Context.
The "Intent" will not pass the reference, but to serialize and deserialize the bitmap object.
I've gone through this: How to send an object from one Android Activity to another using Intents?
But i'm facing problem in sending images in the object to the other activity.
Please help.
Convert your Drawable to Bitmap and send it to Another Activity.
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
To send,
intent.putExtra("Bitmap", bitmap);
To Fetch,
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
Well you can't really send images with intents since you can attach as extras to intents only Serializable objects.
But you can store images (in memory in different structures (like HashMaps, using hashmap will give you some speed optimization for searching that image)) and send notification to other activity to read from hasmap. You can add the key for the image in hashmap as string in extras attached to intent.
Or you can just cache the image and send it's name/path via intents :)
Hope this helps :)
How can I retrieve an image from another Intent within the same application?
If I understood you correctly you can use Intent.putExtra(String, Parcelable) and Intent.getParcelableExtra(String) methods to store and load image. But it's not very fast method. And if you need to transfer a large image, you'd better put it in a static field.