偶遇DiskLruCache(缓存策略解析)

根据郭神的博客写了个小demo,详细的方法都卸载注释里了,先上效果图:

布局代码:

<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.disklrucache.MainActivity" ><ScrollViewandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><TextViewandroid:id="@+id/showSize"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25sp" /><Buttonandroid:id="@+id/read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="缓存图片" /><Buttonandroid:id="@+id/remove"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="移除缓存图片" /><Buttonandroid:id="@+id/delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="删除全部缓存(包括日志文件)"/><Buttonandroid:id="@+id/show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="显示图片" /><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></ScrollView></LinearLayout>

主Activity代码

package com.example.disklrucache;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import libcore.io.DiskLruCache;import android.app.Activity;import android.content.Context;import android.content.pm.PackageInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {String imageUrl = ""; private DiskLruCache lruCache = null ;private TextView showSize ; //缓存区的大小private ImageView img ;private Button read,show ; //读取,与现实在屏幕上private Button remove ; //移除缓存文件但是保留操作日志private Button delete ;//删除缓存@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE) ;setContentView(R.layout.activity_main);this.read = (Button)findViewById(R.id.read) ;this.remove = (Button)findViewById(R.id.remove) ;this.show = (Button)findViewById(R.id.show) ;this.delete = (Button)findViewById(R.id.delete) ;this.showSize = (TextView) findViewById(R.id.showSize) ;img = (ImageView) findViewById(R.id.img) ;File file = getDiskCacheDir(MainActivity.this, "bitmap") ;if(!file.exists()) {file.mkdirs() ;}try {lruCache = DiskLruCache.open(file, getAppVersion(MainActivity.this), 1, 10*1024*1024) ;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.show.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubgetCacheBitmap();showSize.setText("当前缓存尺寸:"+lruCache.size());}});this.read.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubString key = hashKeyForDisk(imageUrl) ;//生成keyDiskLruCache.Editor editor;try {editor = lruCache.edit(key);if(editor != null) {OutputStream out = editor.newOutputStream(0) ;if(downUrlToStream(imageUrl, out)) {editor.commit();} else {editor.abort();}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} //将key传入editor}}).start();}});this.remove.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {String key = hashKeyForDisk(imageUrl);lruCache.remove(key);} catch (IOException e) {e.printStackTrace();}}});this.delete.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {lruCache.delete();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});//getCacheBitmap();//读取下载的图片}/* * 缓存的读取,主要借助get方法 */public void getCacheBitmap() {try {String key = hashKeyForDisk(imageUrl);DiskLruCache.Snapshot snapShot = lruCache.get(key);if (snapShot != null) {InputStream is = snapShot.getInputStream(0);Bitmap bitmap = BitmapFactory.decodeStream(is);img.setImageBitmap(bitmap);}} catch (IOException e) {e.printStackTrace();} }/* * 根据传入的url下载一张图片,,并且将图片写入本地 */private boolean downUrlToStream(String url,OutputStream outputStream) {HttpURLConnection httpurlcon = null ;BufferedInputStream in = null ;BufferedOutputStream out = null ;URL mUrl;try {mUrl = new URL(url);httpurlcon = (HttpURLConnection)mUrl.openConnection() ;in = new BufferedInputStream(httpurlcon.getInputStream(),8*1024) ;out = new BufferedOutputStream(outputStream,1024*8) ;int b ;while((b = in.read()) != -1) {out.write(b);}return true ;} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (final IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(httpurlcon != null) {httpurlcon.disconnect();}try {if(in != null)in.close();if(out != null) {out.close();}}catch(IOException e) {e.printStackTrace();}}return false ;}/* * 调用MD5编码算法生成唯一的key */public String hashKeyForDisk(String key) {String cacheKey ;try {final MessageDigest mDigest = MessageDigest.getInstance("MD5") ;mDigest.update(key.getBytes());cacheKey = bytesToHexString(mDigest.digest()) ;} catch (NoSuchAlgorithmException e) {cacheKey = String.valueOf(key.hashCode()) ;}return cacheKey ;}private String bytesToHexString(byte[] bytes) {StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {String hex = Integer.toHexString(0xFF & bytes[i]);if (hex.length() == 1) {sb.append('0');}sb.append(hex);}return sb.toString(); }/* * 用以获取cache目录,处理当内存卡有异常(如不在或者不可用) */public File getDiskCacheDir(Context context, String uniqueName) {String cachePath;if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())|| !Environment.isExternalStorageRemovable()) {cachePath = context.getExternalCacheDir().getPath();} else {cachePath = context.getCacheDir().getPath();}return new File(cachePath + File.separator + uniqueName);}/* * 得到应用程序的版本号,作为参数传入DiskLruCache的open方法,DiskLruCache默认 * 程序版本号发生变化时移除全部缓存数据 */public int getAppVersion(Context context) {PackageInfo version;try {version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return version.versionCode ;} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return 1 ;//程序发生异常时返回初始版本号1.0}@Overrideprotected void onPause() {// TODO Auto-generated method stubtry {lruCache.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}super.onPause();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubtry {lruCache.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}super.onDestroy();}}最后不要忘了声明权限

有人说,幸福是一种人生的感悟,一种个人的体验。

偶遇DiskLruCache(缓存策略解析)

相关文章:

你感兴趣的文章:

标签云: