开发者

Method to pass an image value?

How would i go about 开发者_运维百科passing an image to the next page for android?


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜