如何将Android图片缓存改写为长尾关键词?

2026-04-02 13:131阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计885个文字,预计阅读时间需要4分钟。

如何将Android图片缓存改写为长尾关键词?

文章来自http://keegan-lee.diandian.com/post/2012-12-06/40047548955,主要记录了一些注释,但未进行其他变化。

文章来自img.558idc.com/uploadfile/allimg/0629/www.diandian.com, 100, outStream);outStream.flush();outStream.close();} catch (FileNotFoundException e) {Log.w("ImageFileCache", "FileNotFoundException");} catch (IOException e) {Log.w("ImageFileCache", "IOException");}}/*** 计算存储目录下的文件大小* 当文件总大小大于规定的CACHE_SIZE或者sdcard剩余空间小于FREE_SD_SPACE_NEEDED_TO_CACHE的规定* 那么删除40%最近没有被使用的文件*/private boolean removeCache(String dirPath) {// 以路径dirPath构造File实例File dir new File(dirPath);// 图片文件数组 dir不是路径时返回nullFile[] files dir.listFiles();// if (files null) {return true;}// sd卡的存储状态必须可读可写否则返回falseif (!android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {return false;}int dirSize 0;for (int i 0; i freeSpaceOnSd()) {// 要移除的文件个数int removeFactor (int) ((0.4 * files.length) 1);// 根据文件修改时间排序Arrays.sort(files, new FileLastModifSort());for (int i 0; i arg1.lastModified()) {return 1;} else if (arg0.lastModified() arg1.lastModified()) {return 0;} else {return -1;}}}}

 

public class ImageMemoryCache {/*** 从内存读取数据速度是最快的为了更大限度使用内存这里使用了两层缓存。* 硬引用缓存不会轻易被回收用来保存常用数据不常用的转入软引用缓存。*/private static final int SOFT_CACHE_SIZE 15; //软引用缓存容量private static LruCache mLruCache; //硬引用缓存private static LinkedHashMap mSoftCache; //软引用缓存public ImageMemoryCache(Context context) {// 通过ActivityManager获取应用的可用内存大小单位mbint memClass ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();// 硬引用缓存容量int cacheSize 1024 * 1024 * memClass / 4; //硬引用缓存容量为系统可用内存的1/4 mLruCache new LruCache(cacheSize) {Overrideprotected int sizeOf(String key, Bitmap value) {if (value ! null)return value.getRowBytes() * value.getHeight();elsereturn 0;}Overrideprotected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {if (oldValue ! null)// 硬引用缓存容量满的时候会根据LRU算法把最近没有被使用的图片转入此软引用缓存mSoftCache.put(key, new SoftReference(oldValue));}};mSoftCache new LinkedHashMap(SOFT_CACHE_SIZE, 0.75f, true) {private static final long serialVersionUID 6040103833179403725L;Overrideprotected boolean removeEldestEntry(Entry eldest) {if (size() > SOFT_CACHE_SIZE){ return true; } return false; }};}/*** 从缓存中获取图片*/public Bitmap getBitmapFromCache(String url) {Bitmap bitmap;//先从硬引用缓存中获取synchronized (mLruCache) {bitmap mLruCache.get(url);if (bitmap ! null) {//如果找到的话把元素移到LinkedHashMap的最前面从而保证在LRU算法中是最后被删除 mLruCache.remove(url);mLruCache.put(url, bitmap);return bitmap;}}//如果硬引用缓存中找不到到软引用缓存中找synchronized (mSoftCache) { SoftReference bitmapReference mSoftCache.get(url);if (bitmapReference ! null) {bitmap bitmapReference.get();if (bitmap ! null) {//将图片移回硬缓存 mLruCache.put(url, bitmap);mSoftCache.remove(url);return bitmap;} else {mSoftCache.remove(url);}}}return null;} /*** 添加图片到缓存*/public void addBitmapToCache(String url, Bitmap bitmap) {if (bitmap ! null) {synchronized (mLruCache) {mLruCache.put(url, bitmap);}}}public void clearCache() {mSoftCache.clear();}}

 

package bitmap.cache;import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;import org.apache.www.cnblogs.com/jinglingJuly/archive/2013/05/21/3089951.html

如何将Android图片缓存改写为长尾关键词?

本文共计885个文字,预计阅读时间需要4分钟。

如何将Android图片缓存改写为长尾关键词?

文章来自http://keegan-lee.diandian.com/post/2012-12-06/40047548955,主要记录了一些注释,但未进行其他变化。

文章来自img.558idc.com/uploadfile/allimg/0629/www.diandian.com, 100, outStream);outStream.flush();outStream.close();} catch (FileNotFoundException e) {Log.w("ImageFileCache", "FileNotFoundException");} catch (IOException e) {Log.w("ImageFileCache", "IOException");}}/*** 计算存储目录下的文件大小* 当文件总大小大于规定的CACHE_SIZE或者sdcard剩余空间小于FREE_SD_SPACE_NEEDED_TO_CACHE的规定* 那么删除40%最近没有被使用的文件*/private boolean removeCache(String dirPath) {// 以路径dirPath构造File实例File dir new File(dirPath);// 图片文件数组 dir不是路径时返回nullFile[] files dir.listFiles();// if (files null) {return true;}// sd卡的存储状态必须可读可写否则返回falseif (!android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {return false;}int dirSize 0;for (int i 0; i freeSpaceOnSd()) {// 要移除的文件个数int removeFactor (int) ((0.4 * files.length) 1);// 根据文件修改时间排序Arrays.sort(files, new FileLastModifSort());for (int i 0; i arg1.lastModified()) {return 1;} else if (arg0.lastModified() arg1.lastModified()) {return 0;} else {return -1;}}}}

 

public class ImageMemoryCache {/*** 从内存读取数据速度是最快的为了更大限度使用内存这里使用了两层缓存。* 硬引用缓存不会轻易被回收用来保存常用数据不常用的转入软引用缓存。*/private static final int SOFT_CACHE_SIZE 15; //软引用缓存容量private static LruCache mLruCache; //硬引用缓存private static LinkedHashMap mSoftCache; //软引用缓存public ImageMemoryCache(Context context) {// 通过ActivityManager获取应用的可用内存大小单位mbint memClass ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();// 硬引用缓存容量int cacheSize 1024 * 1024 * memClass / 4; //硬引用缓存容量为系统可用内存的1/4 mLruCache new LruCache(cacheSize) {Overrideprotected int sizeOf(String key, Bitmap value) {if (value ! null)return value.getRowBytes() * value.getHeight();elsereturn 0;}Overrideprotected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {if (oldValue ! null)// 硬引用缓存容量满的时候会根据LRU算法把最近没有被使用的图片转入此软引用缓存mSoftCache.put(key, new SoftReference(oldValue));}};mSoftCache new LinkedHashMap(SOFT_CACHE_SIZE, 0.75f, true) {private static final long serialVersionUID 6040103833179403725L;Overrideprotected boolean removeEldestEntry(Entry eldest) {if (size() > SOFT_CACHE_SIZE){ return true; } return false; }};}/*** 从缓存中获取图片*/public Bitmap getBitmapFromCache(String url) {Bitmap bitmap;//先从硬引用缓存中获取synchronized (mLruCache) {bitmap mLruCache.get(url);if (bitmap ! null) {//如果找到的话把元素移到LinkedHashMap的最前面从而保证在LRU算法中是最后被删除 mLruCache.remove(url);mLruCache.put(url, bitmap);return bitmap;}}//如果硬引用缓存中找不到到软引用缓存中找synchronized (mSoftCache) { SoftReference bitmapReference mSoftCache.get(url);if (bitmapReference ! null) {bitmap bitmapReference.get();if (bitmap ! null) {//将图片移回硬缓存 mLruCache.put(url, bitmap);mSoftCache.remove(url);return bitmap;} else {mSoftCache.remove(url);}}}return null;} /*** 添加图片到缓存*/public void addBitmapToCache(String url, Bitmap bitmap) {if (bitmap ! null) {synchronized (mLruCache) {mLruCache.put(url, bitmap);}}}public void clearCache() {mSoftCache.clear();}}

 

package bitmap.cache;import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;import org.apache.www.cnblogs.com/jinglingJuly/archive/2013/05/21/3089951.html

如何将Android图片缓存改写为长尾关键词?