views:

53

answers:

2

How would i go about passing an image to the next page for android?

+1  A: 

You have a few different choices here.

I assume by "next page" you mean the next Activity? I also assume you've created a Bitmap out of the image. Bitmap implements Parcelable so you could just put it directly into the extras of the intent. This also lets you can put it in the Bundle in your activity's onSaveInstanceState() method, so it is restored whenever that activity is rotated or is recreated for some other reason.

However, if you have more than one image, this is less than ideal, since you have to serialize and deserialize all your images each time you switch activities. If you obtained the image from the file system somewhere, or if you got it from a ContentProvider like the MediaStore, then you'll have a Uri for it. So, you could just put the Uri in the intent, and then recreate the bitmap each time you load the activity. This has the advantage of being a smaller amount of serialized data, but it's even worse in terms of processing because now you have to read from the filesystem and decompress the image every time.

Therefore, if you are concerned with performance, the only good method is to store the image(s) to a static variable so that it can be accessed by your other activities. This way, both activities actually use the same image instead of duplicating it, thereby saving memory. The only disadvantage with this approach is that you will not be able to start the activity in a new task. All activities that use the image must run in the same process. Also, if you are manually recycling the image when you're done (via the Bitmap.recycle() method), then you'll have to make sure no one else is using the image before you recycle it.

Personally, a lot of my apps download images from a server and I store all HTTP responses in a cache, so whenever I need one of these images I re-request it from the cache. The cache is a singleton so it can be accessed from any of my activities.

Neil Traft
Is it the same way for string values?
User358218
Yes, you either add it to the intent extras, or you store it in a static variable. That's the only way for two activities to share data, any type of data. At least for strings, it's fast to serialize them and they don't take up much memory, so you really have nothing to lose by putting them into the intent.
Neil Traft
I should also add that if you don't need multiple activites to share an object, but you do need to preserve that object when the activity gets recreated due to rotation, then you should check out the `Activity.onRetainNonConfigurationInstance()` method. http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance
Neil Traft
Ok thanks for your help.
User358218
You shouldn't pass large images through bundles, there's a limit to how much data can be passed in a bundle. Recreating the image on the other side using the image's URI or gaining access to the allocated Bitmap through global state are better approaches.
Josh
Agreed. I didn't know there was actually a limit. +1
Neil Traft
A: 

Do you have a sample code for passing an image value? Will this work?

Intent i = new Intent(Moods.this, New_Entry.class);
                    Bundle f = new Bundle();
                    f.putString("image", img);  
UserA
No, you don't create the `Bundle` yourself, it's already created by the intent. Just add your image like so: `i.putExtra("image", img)` ... But instead of doing this, I would take the advice of Josh above, and store it in a global static variable somewhere.
Neil Traft
Oh alright thanks. But the global static variable method is very complicated for me. I'm really new to android.
UserA