图片的三级缓存
2016-06-27 08:46
337 查看
//SD卡的写和读的权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.support.v4.util.LruCache;import android.util.Log;/*** Created by Administrator on 2016/6/13 0013.** 内存缓存*/public class MemoryCache {//内存管理器private LruCache<String,Bitmap>lruCache;//类似于Map集合public MemoryCache(){//构造方法int maxSize;//获得本机(可用)内存的大小long maxMemory=Runtime.getRuntime().maxMemory();//分配空间给该应用作为内存(八分之一)maxSize=(int)(maxMemory/8);Log.i("doInBackground","!!!!!!!!!本机的内存空间为:"+maxMemory);Log.i("doInBackground","!!!!!!!!!!本机的可用内存空间为:"+maxSize);lruCache=new LruCache<String,Bitmap>(maxSize){@Override//该方法返回一张图片的字节总数protected int sizeOf(String key, Bitmap value) {return super.sizeOf(key, value);}};}//将图片放到内存中的方法public void putImage(String path,Bitmap bitmap){lruCache.put(path,bitmap);}//从内容中取出图片public Bitmap getImage(String path){return lruCache.get(path);}}
</pre><pre name="code" class="java">
<pre name="code" class="java">package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;/*** Created by Administrator on 2016/6/13 0013.** 本地(SD卡)缓存*/public class LocalCache {public static final String derictory= Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+"cbk";//从本地(SD卡)取出指定的图片public Bitmap getBitmap(String imageName){Bitmap bitmap=null;File file=new File(imageName);if(file.exists()){try {bitmap= BitmapFactory.decodeStream(new FileInputStream(file));} catch (FileNotFoundException e) {e.printStackTrace();}}return bitmap;}//将图片放到SD卡中public void setBitmap(String imageName,Bitmap bitmap){File file=new File(derictory,imageName);//判断父目录是否存在File parentFile=file.getParentFile();if(!parentFile.exists()){//如果目录不存在parentFile.mkdirs();//创建目录}try {//把图片写入SD卡中bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));} catch (FileNotFoundException e) {e.printStackTrace();}}}
package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.os.AsyncTask;import android.widget.ImageView;/*** Created by Administrator on 2016/6/13 0013.* 网络请求*/public class NetCache {private LocalCache localCache;//本地SDprivate MemoryCache memoryCache;//内存//提供构造方法public NetCache(LocalCache localCache, MemoryCache memoryCache) {this.localCache = localCache;this.memoryCache = memoryCache;}//发起网络请求的方法public void downImage(String path,ImageView imageView){new ImageAysncTask(imageView).execute(path);//创建异步任务}//异步任务类class ImageAysncTask extends AsyncTask<String,Void,Bitmap>{private ImageView imageView;private String path;public ImageAysncTask(ImageView imageView) {this.imageView=imageView;}@Overrideprotected Bitmap doInBackground(String... params) {path=params[0];//下载网络图片Bitmap bitmap=MyHttp.downImageFromNet(path,imageView);return bitmap;}@Overrideprotected void onPostExecute(Bitmap bitmap) {if(bitmap!=null){//判断ImageView是否重用,解决错位的问题if(imageView.getTag().equals(path)){imageView.setImageBitmap(bitmap);//给图片控件设置显示的图片}//把下载的图片保存到SD卡中String filename=null;filename=path.substring(path.lastIndexOf("/")+1);localCache.setBitmap(filename,bitmap);//把图片保存到内存中memoryCache.putImage(path,bitmap);}}}}
package com.example.administrator.app1.utils;import android.graphics.Bitmap;import android.widget.ImageView;/*** Created by Administrator on 2016/6/13 0013.** 图片缓存的管理类*/public class BitmapCache {private LocalCache localCache;//SD卡private MemoryCache memoryCache;//内存private NetCache netCache;public static BitmapCache bitmapCache=new BitmapCache();public static BitmapCache getInstance(){return bitmapCache;//提供方法,返回该对象(单利模式)}private BitmapCache(){//初始化内存,SD卡和网络请求localCache=new LocalCache();memoryCache=new MemoryCache();netCache=new NetCache(localCache,memoryCache);}//三级缓存的方法public void setImageViewFromCache(String path,ImageView imageView){//1、先从内存中取图片Bitmap bitmap=memoryCache.getImage(path);if(bitmap!=null){//如果缓存中有该图片imageView.setImageBitmap(bitmap);return;}//2、如果内存中没有就到SD卡中找找String filename=null;//获得文件名filename=path.substring(path.lastIndexOf("/")+1);bitmap=localCache.getBitmap(filename);if(bitmap!=null){//获得压缩的图片memoryCache.putImage(path,bitmap);//保存到内存中imageView.setImageBitmap(bitmap);//设置为图片控件显示的图片return;}//3、如果到这里来,就需要去网络下载图片了netCache.downImage(path,imageView);}}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories