您的位置:首页 > 移动开发 > Android开发

图片的三级缓存

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);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息