您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 图片缓存机制代码
  所属分类: Android
  开发工具:
  文件大小: 2mb
  下载次数: 0
  上传时间: 2016-01-27
  提 供 者: wx_***
 详细说明: 用于类似图库,缓存,所困、缩略图 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; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 图片缓存
 输入关键字,在本站1000多万海量源码库中尽情搜索: