android 缓存实现
1。之前因为做一个项目的过程中遇到要频繁重复下载的文件比如图片等,需要在本地缓存,除了用户体验也保证了省流量。
这个demo是用下载网络图片来演示。
一共有六张网络图片,加载图片时,会判断图片是否下载到本地的文件夹中,如果下载了,直接从文件中读取,如果没有就会去下载,下载完之后通过一个handler通知adapter的notifyDataSetChanged()方法,来对listActivity进行通知,来显示对应的列表。
具体代码如下;
public class CatheAdapter extends BaseAdapter {
private List<String> mUrl = new ArrayList<String>();
private LayoutInflater mInflater;
private Context context;
public CatheAdapter(Context context) {
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/2.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/3.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/4.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/5.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/6.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/7.png");
this.context = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mUrl.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item,null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
image.setImageBitmap(getBitmap(position));
return convertView;
}
private Bitmap getBitmap(int position){
Bitmap bitmap = null;
//File dir = Environment.getExternalStorageDirectory();
String fileName = File.separator+"sdcard"+File.separator+mUrl.get(position).hashCode();
if (new File(fileName).exists()) {
bitmap = BitmapFactory.decodeFile(fileName);
}else {
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
new DownLoadImage(this, mUrl.get(position)).start();
}
return bitmap;
}
}
这是adaopter类的代码。
public class DownLoadImage extends Thread{
private CatheAdapter catheAdapter;
private String mUrl;
private String fileName;
private File dir;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
catheAdapter.notifyDataSetChanged();
}
};
public DownLoadImage(CatheAdapter catheAdapter, String mUrl) {
this.catheAdapter = catheAdapter;
this.mUrl = mUrl;
//dir = Environment.getExternalStorageDirectory();
fileName = File.separator+"sdcard"+File.separator+mUrl.hashCode();
}
@Override
public void run() {
URL url;
try {
url = new URL(mUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setUseCaches(false);
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer))>-1) {
fos.write(buffer,0,count);
}
fos.flush();
fos.close();
is.close();
conn.disconnect();
handler.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是下载图片的类,图片放在了sdcard的下面,因为是演示所以这么写了。建议大家如果要新建一个文件。
最后是主要的类。
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setListAdapter(new CatheAdapter(this));
}
}
布局代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="liu.example.cathetest.MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
这是每一个item的布局代码;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="liu.example.cathetest.MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
最后主要权限,除了Internet权限还有访问sdcard的权限。
android 缓存实现的更多相关文章
- Android 缓存
1.Android缓存机制&一个缓存框架推荐 http://blog.csdn.net/shakespeare001/article/details/51695358 2.ASimpleCac ...
- Android缓存处理
Android缓存: 採用缓存,能够进一步大大缓解数据交互的压力,又能提供一定的离线浏览.下边我简略列举一下缓存管理的适用环境: 1. 提供网络服务的应用 2. 数据更新不须要实时更新.哪怕是3-5分 ...
- android缓存具体解释
Android缓存: 採用缓存,能够进一步大大缓解数据交互的压力.又能提供一定的离线浏览.下边我简略列举一下缓存管理的适用环境: 1. 提供网络服务的应用 2. 数据更新不须要实时更新,哪怕是3-5分 ...
- 【转】彻底解析Android缓存机制——LruCache
彻底解析Android缓存机制——LruCache 关于Android的三级缓存,其中主要的就是内存缓存和硬盘缓存.这两种缓存机制的实现都应用到了LruCache算法,今天我们就从使用到源码解析,来彻 ...
- Android缓存学习入门(二)
本文主要包括以下内容 内存缓存策略 文件缓存策略 内存缓存策略 当有一个图片要去从网络下载的时候,我们并不会直接去从网络下载,因为在这个时代,用户的流量是宝贵的,耗流量的应用是不会得到用户的青睐的.那 ...
- Android缓存学习入门
本文主要包括以下内容 利用LruCache实现内存缓存 利用DiskLruCache实现磁盘缓存 LruCache与DiskLruCache结合实例 利用了缓存机制的瀑布流实例 内存缓存的实现 pub ...
- Android 缓存目录 Context.getExternalFilesDir()和Context.getExternalCacheDir()方法
一.基础知识 应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载 ...
- Android缓存技术
android应用程序中 1. 尽可能的把文件缓存到本地.可以是 memory,cache dir,甚至是放进 SD 卡中(比如大的图片和音视频). 可以设置双重缓冲,较大的图片或者音频放到SD ...
- android 缓存Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...
- android缓存之Lrucache 和LinkedHashMap
两者的区别 网上有很多人使用软引用加载图片的多 ,但是现在已经不再推荐使用这种方式了,(1)因为从 Android 2.3 (API Level 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的 ...
随机推荐
- 十大面试难题解惑,看完秒杀一切 HR 面。程序员必读!
最能体现求职者能力的就是面试,能不能拿到Offer,取决于你面试时的表现,只有有准备才能在面试过程中游刃有余. 小编收集了10个面试官最爱提的问题,虽然题目千变万化,但是万变不离其宗,只要掌握了答题的 ...
- [SCOI 2012]滑雪与时间胶囊
Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...
- bzoj 2004: [Hnoi2010]Bus 公交线路
Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距 离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决 ...
- poj1753 高斯消元
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37055 Accepted: 16125 Descr ...
- 抽象方法不能是static或native或synchroniz
abstract 是抽象了,只有声明,没有具体的实现方法 static是静态的,是一种属于类而不属于对象的方法或者属性,而我们知道,类其实也是一个对象,他是在class文件加载到虚拟机以后就会产生的对 ...
- python变量、条件循环语句
1. 变量名 - 字母 - 数字 - 下划线 #数字不能开头:不能是关键字:最好不好和python内置的函数等重复 2. 条件语句 缩进用4个空格(Tab键)注意缩进如果是空格键和Tab键混用, ...
- Python Django系统
本节内容 路由系统,视图函数,模板引擎,ORM操作 FBV和CBV ORM操作补充 Cookie和Session Ajax入门 1. Django基本内容整理 1.1 路由系统 Django中路由系 ...
- Enum枚举
Java Enum原理 public enum Size{ SMALL, MEDIUM, LARGE, EXTRA_LARGE }; 实际上,这个声明定义的类型是一个类,它刚好有四个实例,在此尽量不要 ...
- MySQL的Explain关键字查看是否使用索引
explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句.简单讲,它的作用就是分析查询性能. explain关键字的使用方法很简单,就是 ...
- 浅谈Log4net在项目中如何记录日志
一 引入背景 在软件开发周期中,无论是开发中,或是测试中,或是上线后,选择合适的工具监控程序的运行状态至关重要,只有如此,才能更好地排查程序问题和检测程序性能问题等.本篇文章主要与大家分享,如何 ...