我们不能每次加载图片的时候都让用户从网络上下载,这样不仅浪费流量又会影响用户体验,所以Android中引入了图片的缓存这一操作机制。

原理:

  首先根据图片的网络地址在网络上下载图片,将图片先缓存到内存缓存中,缓存到强引用中 也就是LruCache中。如果强引用中空间不足,就会将较早存储的图片对象驱逐到软引用(softReference)中存储,然后将图片缓存到文件(内部存储外部存储)中;读取图片的时候,先读取内存缓存,判断强引用中是否存在图片,如果强引用中存在,则直接读取,如果强引用中不存在,则判断软引用中是否存在,如果软引用中存在,则将软引用中的图片添加到强引用中并且删除软引用中的数据,如果软引用中不存在,则读取文件存储,如果文件存储不存在,则网络加载。
  下载: 网络--内存--文件
  读取: 内存--强引用--软引用--文件--网络
也就是这样的一个过程,下面用一个简单地demo来演示一下图片你的三级缓存,此demo中只有一个界面,界面上一个ImageView用来显示图片,一个按钮用来点击的时候加载图片。布局如下:
  

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn_download"
android:layout_below="@+id/iv_img"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载图片"/> </RelativeLayout>

  因为要从网络下载数据,还要存储到本地sd卡中,所以不要忘了为程序添加网络访问的权限、网络状态访问的权限和向外部存储设备写内容的权限:

     <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  接着,创建一个 HttpUtils 工具类用于访问网络,代码如下:

 package com.yztc.lx.cashimg;

 import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; /**网络访问工具类
* Created by Lx on 2016/8/19.
*/ public class HttpUtils {
/**
* 判断网络连接是否通畅
* @param mContext
* @return
*/
public static boolean isNetConn(Context mContext) {
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
return info.isConnected();
} else {
return false;
}
} /**
* 根据path下载网络上的数据
* @param path 路径
* @return 返回下载内容的byte数据形式
*/
public static byte[] getDateFromNet(String path) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.connect();
if (conn.getResponseCode()==200) {
InputStream is = conn.getInputStream();
byte b[] = new byte[1024];
int len;
while ((len=is.read(b))!=-1) {
baos.write(b, 0, len);
}
return baos.toByteArray();
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
} }

  还有操作外部存储的工具类:

 package com.yztc.lx.cashimg;

 import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* Created by Lx on 2016/8/20.
*/ public class ExternalStorageUtils {
/**
* 将传递过来的图片byte数组存储到sd卡中
* @param imgName 图片的名字
* @param buff byte数组
* @return 返回是否存储成功
*/
public static boolean storeToSDRoot(String imgName, byte buff[]) {
boolean b = false;
String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(basePath, imgName);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(buff);
fos.close();
b = true;
} catch (IOException e) {
e.printStackTrace();
}
return b;
} /**
* 从本地内存中根据图片名字获取图片
* @param imgName 图片名字
* @return 返回图片的Bitmap格式
*/
public static Bitmap getImgFromSDRoot(String imgName) {
Bitmap bitmap = null;
String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(basePath, imgName);
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte b[] = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
baos.write(b, 0, len);
}
byte buff[] = baos.toByteArray();
if (buff != null && buff.length != 0) {
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
} }

  本例中将图片默认存在了sd卡根目录中。

  然后是最主要的主函数了:

 package com.yztc.lx.cashimg;

 import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast; import java.lang.ref.SoftReference;
import java.util.LinkedHashMap; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_download;
private ImageView iv_img;
private MyLruCache myLruCache;
private LinkedHashMap<String, SoftReference<Bitmap>> cashMap = new LinkedHashMap<>();
private static final String TAG = "MainActivity";
private String imgPath = "http://www.3dmgame.com/UploadFiles/201212/Medium_20121217143424221.jpg";
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
iv_img.setImageBitmap(bitmap);
Toast.makeText(MainActivity.this, "从网络上下载图片", Toast.LENGTH_SHORT).show();
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
int totalMemory = (int) Runtime.getRuntime().maxMemory();
int size = totalMemory / 8;
myLruCache = new MyLruCache(size);
btn_download.setOnClickListener(this);
} private void initView() {
btn_download = (Button) findViewById(R.id.btn_download);
iv_img = (ImageView) findViewById(R.id.iv_img);
} @Override
public void onClick(View v) {
Bitmap b = getImgCache();
if (b != null) {
iv_img.setImageBitmap(b);
} else {
new Thread(new Runnable() {
@Override
public void run() {
if (HttpUtils.isNetConn(MainActivity.this)) {
byte b[] = HttpUtils.getDateFromNet(imgPath);
if (b != null && b.length != 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
myLruCache.put(imgPath, bitmap);
Log.d(TAG, "run: " + "缓存到强引用中成功");
boolean bl = ExternalStorageUtils.storeToSDRoot("haha.jpg", b);
if (bl) {
Log.d(TAG, "run: " + "缓存到本地内存成功");
} else {
Log.d(TAG, "run: " + "缓存到本地内存失败");
}
} else {
Toast.makeText(MainActivity.this, "下载失败!", Toast.LENGTH_SHORT).show();
} } else {
Toast.makeText(MainActivity.this, "请检查你的网络!", Toast.LENGTH_SHORT).show();
}
}
}).start();
}
} /**
* 从缓存中获取图片
*
* @return 返回获取到的Bitmap
*/
public Bitmap getImgCache() {
Bitmap bitmap = myLruCache.get(imgPath);
if (bitmap != null) {
Log.d(TAG, "getImgCache: " + "从LruCache获取图片");
} else {
SoftReference<Bitmap> sr = cashMap.get(imgPath);
if (sr != null) {
bitmap = sr.get();
myLruCache.put(imgPath, bitmap);
cashMap.remove(imgPath);
Log.d(TAG, "getImgCache: " + "从软引用获取图片");
} else {
bitmap = ExternalStorageUtils.getImgFromSDRoot("haha.jpg");
Log.d(TAG, "getImgCache: " + "从外部存储获取图片");
}
}
return bitmap;
} /**
* 自定义一个方法继承系统的LruCache方法
*/
public class MyLruCache extends LruCache<String, Bitmap> { /**
* 必须重写的构造函数,定义强引用缓存区的大小
* @param maxSize for caches that do not override {@link #sizeOf}, this is
* the maximum number of entries in the cache. For all other caches,
* this is the maximum sum of the sizes of the entries in this cache.
*/
public MyLruCache(int maxSize) {
super(maxSize);
} //返回每个图片的大小
@Override
protected int sizeOf(String key, Bitmap value) {
//获取当前变量每行的字节数和行高度(基本是固定写法,记不住给我背!)
return value.getRowBytes() * value.getHeight();
} /**
* 当LruCache中的数据被驱逐或是移除时回调的函数
*
* @param evicted 当LruCache中的数据被驱逐用来给新的value倒出空间的时候变化
* @param key 用来标示对象的键,一般put的时候传入图片的url地址
* @param oldValue 之前存储的旧的对象
* @param newValue 存储的新的对象
*/
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
if (evicted) {
/**
* 将旧的值存到软引用中,因为强引用中可能有多个值被驱逐,
* 所以创建一个LinkedHashMap<String, SoftReference<Bitmap>>来存储软引用
* 基本也是固定写法
*/
SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(oldValue);
cashMap.put(key, softReference);
}
}
}
}

  基本的思路都在代码注释中写的很详细了,主要就是要自定义一个类,来继承系统的LruCache,实现其中的两个主要的方法sizeOf()和entryRemoved(),还有就是必须重写它的构造函数。

简单地Android中图片的三级缓存机制的更多相关文章

  1. Android中图片的三级缓存

    为什么要使用三级缓存 如今的 Android App 经常会需要网络交互,通过网络获取图片是再正常不过的事了 假如每次启动的时候都从网络拉取图片的话,势必会消耗很多流量.在当前的状况下,对于非wifi ...

  2. Android中图片的三级缓存策略

    在开发过程中,经常会碰到进行请求大量的网络图片的样例.假设处理的不好.非常easy造成oom.对于避免oom的方法,无非就是进行图片的压缩.及时的回收不用的图片.这些看似简单可是处理起来事实上涉及的知 ...

  3. android中图片的三级缓存cache策略(内存/文件/网络)

    实现图片缓存也不难,需要有相应的cache策略.这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且 ...

  4. 关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析

    原文:关于Android中图片大小.内存占用与drawable文件夹关系的研究与分析 相关: Android drawable微技巧,你所不知道的drawable的那些细节 经常会有朋友问我这个问题: ...

  5. Android中图片占用内存的计算

    Android中图片占用内存的计算 原文链接 http://blog.sina.com.cn/s/blog_4e60b09d01016133.html   在Android开发中,我现在发现很多人还不 ...

  6. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

  7. Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)

    在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...

  8. (转)Android中图片占用内存计算

    在Android开发中,我现在发现很多人还不会对图片占用内存进行很好的计算.因此撰写该博文来做介绍,期望达到抛砖引玉的作用.   Android中一张图片(BitMap)占用的内存主要和以下几个因数有 ...

  9. android中图片倒影、圆角效果重绘

    本文用来记录一些Android 操作图片的方法,方便查看. 1.将Drawable转化为Bitmap public static Bitmap drawableToBitmap(Drawable dr ...

随机推荐

  1. 高性能MySQL笔记-第1章MySQL Architecture and History-001

    1.MySQL架构图 2.事务的隔离性 事务的隔离性是specific rules for which changes are and aren’t visible inside and outsid ...

  2. 文件格式PDF

    pdf(Portable Document Format的简称,意为“便携式文档格式”),是由Adobe Systems用于与应用程序.操作系统.硬件无关的方式进行文件交换所发展出的文件格式.PDF文 ...

  3. python xml.etree ElementTree解析 编辑 xml

    python有很多种xml解析方式,不过感觉etree的ElementTree 用起来最方便. #coding=utf-8 from xml.etree import ElementTree impo ...

  4. Path Sum的变体

    早上看到一个面经题跟Path Sum很像, 给一个TreeNode root和一个target,找到一条从根节点到leaf的路径,其中每个节点和等于target. 与Path Sum不同是, Path ...

  5. swift:入门知识之协议与扩展

    swift中使用protocol声明一个协议接口 swift中类.枚举和结构体都可以实现协议接口 swift中类中的方法都可以修改成员变量的值 swift中结构体中的方法默认是不能修改成员变量的,添加 ...

  6. Linux远程文件传输

    linux系统中,难免会遇到一些要将某文件通过网络传送给其他主机的情况,而恰好两台主机 都是linux系统的时候,我们就可以直接使用scp命令来传输文件到另一台主机了. scp命令用于在网络中安全的传 ...

  7. Oracle ->> 行转列, 列转行

    除了Pivot和Unpivot这两个函数,还有像CASE WHEN + 聚合函数像MAX,SUM这类的来完成.今天发现Oracle下居然有这样一个和SQL SERVER 2012以后新增的新函数叫II ...

  8. perl install module as non-root user

    install to local directory. 1. cpan 初始化,不用local::lib,mannual就行,其他auto2. 修改cpan 配置文件 cpan > o conf ...

  9. 51nod 1050 循环数组最大子段和 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050&noticeId=13385 参考:http://blog. ...

  10. NDK(8)"Unknown Application ABI"的解决方案

    ndk 调试本地应用时 报错如下 : console信息: [2015-08-17 19:52:05 - NdkSample] Unknown Application ABI: [2015-08-17 ...