图片缓存机制代码

时间:2019-02-16 03:58:59
【文件属性】:

文件名称:图片缓存机制代码

文件大小:2.04MB

文件格式:ZIP

更新时间:2019-02-16 03:58:59

图片缓存

用于类似图库,缓存,所困、缩略图 package com.example.cache; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.util.DisplayMetrics; /** * 1.从内存中加载 * 2.本地缓存中加载 * 3.本地加载 * @author Administrator * */ public class LoadCacheImageTool { private Activity activity; private Map> cacheMap; public LoadCacheImageTool(Activity activity){ this.activity = activity; this.cacheMap = new HashMap>(); } public Bitmap loadCacheImage(String imagePath){ Bitmap bitmap = null; if (cacheMap.containsKey(imagePath)) { bitmap = cacheMap.get(imagePath).get(); if (bitmap!=null) { return bitmap; } } bitmap = loadLocalCacheImage(imagePath); cacheMap.put(imagePath, new SoftReference(bitmap)); return bitmap; } ///mnt/sdcard/bk.png ///mnt/sdcard/cache/bk.png.cache private Bitmap loadLocalCacheImage(String imagePath) { Bitmap bitmap = null; String cacheImagePath = getCacheImagePath(imagePath); File cacheFile = new File(cacheImagePath); if (!cacheFile.exists()) { bitmap = loadLocalBigImage(imagePath); saveToCacheDir(bitmap,cacheImagePath); }else{ try { bitmap = BitmapFactory.decodeStream(new FileInputStream(cacheFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return bitmap; } private String getCacheImagePath(String imagePath) { String cacheDir = new File(imagePath).getParent()+"/cache/"; if (!new File(cacheDir).exists()) { new File(cacheDir).mkdirs(); } String newImageName = new File(imagePath).getName()+".cache"; String newImagePath = cacheDir+newImageName; return newImagePath; } private void saveToCacheDir(Bitmap bitmap, String cacheImagePath) { FileOutputStream fos = null; try { fos = new FileOutputStream(cacheImagePath); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)) { fos.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } public Bitmap loadLocalBigImage(String imagePath){ DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth = metrics.widthPixels; int screenHeight = metrics.heightPixels; System.out.println("screenWidth:"+screenWidth+"=="+screenHeight); Options options = new Options(); //加载边框 一定options.inJustDecodeBounds = true; options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); int imageWidth = options.outWidth; int imageHeight = options.outHeight; System.out.println("imageWidth:"+imageWidth+"=="+imageHeight); int scaleX = (int) Math.ceil(imageWidth/(float)screenWidth); int scaleY = (int) Math.ceil(imageHeight/(float)screenHeight); if (scaleX>1||scaleY>1) { options.inSampleSize = (scaleX>scaleY)?scaleX:scaleY; } System.out.println("inSamPle:"+options.inSampleSize); options.inJustDecodeBounds =false; //图片的质量 options.inPreferredConfig = Bitmap.Config.ARGB_4444; //内存上的处理 可删除 options.inInputShareable = true; options.inPurgeable = true; try { bitmap = BitmapFactory.decodeStream(new FileInputStream(imagePath), null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; } }


【文件预览】:
20150724
----缓存图片()
--------.project(848B)
--------project.properties(563B)
--------src()
--------AndroidManifest.xml(1KB)
--------res()
--------assets()
--------gen()
--------libs()
--------.classpath(475B)
--------proguard-project.txt(781B)
--------ic_launcher-web.png(50KB)
--------bin()
----ͼƬ()
--------.project(842B)
--------project.properties(563B)
--------src()
--------AndroidManifest.xml(951B)
--------res()
--------assets()
--------gen()
--------libs()
--------.classpath(475B)
--------proguard-project.txt(781B)
--------ic_launcher-web.png(50KB)
--------bin()

网友评论