DiskLruCache使用指南
2016-05-24 11:35
337 查看
效果图
DiskLruCache下载
下载DisLruCache类,复制到项目中使用代码
public class DisLruCacheActivity extends BaseActivity { private DiskLruCache mDiskLruCache; private Context context; private ImageView imageView; private String imageUrl = "http://img02.sogoucdn.com/app/a/100520024/3083d67034424958852fa8a3c2fcd656"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_retrofit); context = this; imageView = (ImageView) findViewById(R.id.iv_new); try { File cacheDir = getDiskCacheDir(context, "bitmap"); if (!cacheDir.exists()) { cacheDir.mkdirs(); } //获取DiskLruCache对象 mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024); } catch (IOException e) { e.printStackTrace(); } //获取缓存对象 try { String key = hashKeyForDisk(imageUrl); DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key); if (snapShot != null) { InputStream is = snapShot.getInputStream(0); Bitmap bitmap = BitmapFactory.decodeStream(is); imageView.setImageBitmap(bitmap); } } catch (IOException e) { e.printStackTrace(); } //写入缓存对象 new Thread(new Runnable() { @Override public void run() { try { String key = hashKeyForDisk(imageUrl); DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { OutputStream outputStream = editor.newOutputStream(0); if (downloadUrlToStream(imageUrl, outputStream)) { editor.commit(); } else { editor.abort(); } } mDiskLruCache.flush(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } //下载图片 private boolean downloadUrlToStream(String urlString, OutputStream outputStream) { HttpURLConnection urlConnection = null; BufferedOutputStream out = null; BufferedInputStream in = null; try { final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024); out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) { out.write(b); } return true; } catch (final IOException e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (final IOException e) { e.printStackTrace(); } } return false; } //生成MD5编码 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(); } //获取缓存路径 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); } //获取版本号 public int getAppVersion(Context context) { try { PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return info.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return 1; } }
精彩推荐:Android DiskLruCache完全解析,硬盘缓存的最佳方案
相关文章推荐
- 深入理解 Java 垃圾回收机制
- C++中的析构函数
- php上传文件及头像预览
- JavaScript概览(一)--基本语法
- Linux命令大全
- json与泛型互转和json与datatable互转
- hdu2535:Vote
- Openstack liberty源码分析 之 云主机的启动过程1
- Codeforces Round #340 (Div. 2) E 莫队算法
- MySQL数据库表操作的经典语句
- 二叉树的遍历
- android 在非UI线程更新UI仍然成功原因深入剖析
- PHP实现同一个帐号不允许多人同时重复登陆
- css的核心内容 标准流、盒子模型、浮动、定位等分析
- MVP in Android,让Activity更优雅一点。
- unity3d 新UI系统UGUI点击与3D场景点击分离
- asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析
- android 字符串操作
- Typeface 篇
- Largest Number