安卓:分享活动中的图像缓存 [英] Android: sharing an image cache among activities

查看:93
本文介绍了安卓:分享活动中的图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,并不能完全弄清楚实施可在多个活动中共享2级缓存图像的最佳方式瓦特/在一个单一的应用程序。

I am developing an Android application and can't quite figure out the best way of implementing a 2-level image cache that can be shared among multiple activities w/in a single application.

例如:

应用程序有3个活动(A,B和C)和参数的缘故可以说,A调用B和B调用C和C调用A.每个活动显示从网上下载一个图像,我使用AsyncTask的下载和显示图像瓦特/每一项活动 - 很容易的。现在,我想补充的图像缓存,以避免在同一图像的多个下载。

Application has 3 activities (A, B, and C) and for the sake of argument lets say that A calls B and B calls C and C calls A. Each activity displays an image downloaded from the web and I'm using asynctask to download and display images w/in each activity - easy enough. Now I'd like to add an image cache to avoid multiple downloads of the same image.

现在每次活动开始一个简单的AsyncTask的下载图像,并适当地更新视图的新实例。显然,它很容易更新基本的AsyncTask着手下载,一旦下载完成,但我卡在如何/在哪里创建和初始化缓存更新缓存前检查图像缓存。任何想法,将AP preciated。

Right now each activity starts a new instance of a simple asynctask that downloads the image and updates the view appropriately. Obviously its easy enough to update the basic asynctask to check the image cache before proceeding to download and to update the cache once the download is complete but I'm stuck on how/where to create and initialize the cache. Any thoughts would be appreciated.

推荐答案

您可以添加到您的应用程序的清单:

You can add this to your app's manifest:

应用软件Android:名称
  =所有MyApplication(......)

application android:name ="MyApplication" (...)

您可以创建具有名为所有MyApplication一类。那么你可以使用这个类在整个活动。检查,使异步调用之前,如果你已经使用正确的图像。如果你有,你可以使用缓存的一个,如果不是,你可以得到一个新的。您可以尝试(在这种情况下获得一些随机字符串)是这样的:

You can then create a class that has the name "MyApplication". You can then use that class across your activities. Check, before making the async call, if you already have a proper image to use. If you have, you use the one "cached", if not, you can get a new one. You can try something like this (in this case to get some random strings):

ArrayList的myStrings =
  ((我的应用程序)
  this.getApplication())getRandomStrings();

ArrayList myStrings = ((MyApplication) this.getApplication()).getRandomStrings();

希望这有助于你。 :)

Hope this helped you. :)

编辑:不要忘记将创建所有MyApplication是这样的:

Don't forget to the create your "MyApplication" like this:

公共类MyApplication的扩展
  应用

public class MyApplication extends Application

这篇关于安卓:分享活动中的图像缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆