Android中图片的三级缓存策略
在開發過程中,經常會碰到進行請求大量的網絡圖片的樣例。假設處理的不好。非常easy造成oom。對于避免oom的方法,無非就是進行圖片的壓縮。及時的回收不用的圖片。這些看似簡單可是處理起來事實上涉及的知識特別廣泛。在這里主要解說圖片的緩存,通過緩存也是個非常好的避免oom的途徑。近期經常使用的到自然是LruCache了,它里面有一個LindedHashMap鏈式表,并且這個表是按近期最少使用算法排序的,近期使用的往往拍的靠前。最少使用的往往處于隊尾,當須要回收利用的時候,最后面的那個元素是會被清除掉的。LruCache主要實現了內存緩存,這里還會解說DiskLruCache,這是GitHub開源庫提供的使用文件緩存的一種基于近期最少使用的硬盤緩存類。
同一時候順帶解說圖片的簡單壓縮方法。那么,接下來就須要了解LruCache,DiskLruCache,BitmapFactory。
一、LruCache
LruCache是Android中提供的基于近期最少使用算法的緩存策略,它能夠對一定數量的值持有強引用。近期最少使用算法體如今,當有一個值被訪問的時候,這個值就會被移動到隊列的對頭,而當一個值加入的時候恰好達到LruCache申請的緩存空間。那么處于隊尾的值就會被踢出隊列。由于該值不再是緩存cache持有的對象,所以一旦垃圾回收器須要回收內存的時候,該值就會由于處于回收機制考慮的對象而可能被回收。
使用Lrucache有非常多細節要注意。你應該重寫create方法,這樣即使在內存中找不到key相應的值,也能又一次創建一個。應該重寫sizeOf方法。由于這種方法默認是返回緩存中實體的數量的。而不是占用空間的大小。這個類是線程安全的。當我們調用get方法的時候,系統會幫我們進行系統同步的。
可是注意。假設你自己重寫了create方法的話,create的方法并非線程安全的。可是由于get方法里面進行加入值對象的時候會推斷是否發生沖突。所以我們不須要考慮線程安全的問題。
以下具體解說LruCache的各方面。方便后面的使用。
①、LruCache的構造函數解析,首先看源代碼:
public LruCache(int maxSize) {if (maxSize <= 0) {throw new IllegalArgumentException("maxSize <= 0");}this.maxSize = maxSize;this.map = new LinkedHashMap<K, V>(0, 0.75f, true);}
1、maxSize:當我們使用構造函數的時候。假設沒有重寫sizeOf方法的話,這個值代表在此cache里面能夠緩存的實體個數(Bitmap,int,String......)的最大值。假設重寫了sizeOf方法的話。這個值代表全部緩存實體所占用的字節大小的總和。此值必須大于0.
2、map:這是LruCache里面用于存放實體對象的鏈式表,前面說了,Lrucache是基于近期最少使用算法來決定淘汰哪個實體的,這個算法事實上不是LruCache實現的,而是LinkedHashMap實現的。
在這里,map=new LinkedHashMap中的第三個參數。假設傳值true的話,那么鏈式表里面的對象會依據近期最少使用算法來排序,近期最少使用的對象。就越往后。假設傳值false的話,就是按插入值得順序在鏈式表中排序。
②、又一次設置Lrucache對象緩存大小的最大值:
此方法用于又一次這是緩存大小。當新的maxSize比原來的值要大的時候,僅僅是將新的值賦值給了原來的值,并沒有做其它的事情。而當新的值比原來的值小的時候。那么就會把已經緩存的對象從隊列里移除。直到緩存的大小小于或者等于新的maxSize的值。
③、依據key來創建一個值:
所以,假設創建值的時候發生了沖突,那么新創建的值就會被丟棄,否則就會被壓進鏈式表的表頭。
④、依據key來獲取一個值:
依據key從緩存中獲取相應的值,假設緩存中存在該值就返回。假設不存在,就會調用create方法去創建一個值,注意。假設你沒有重寫這種方法的話。默認返回的都是null值。
假設create了一個新的值(不能為null)且不與原來cache里面的實體產生沖突的話,就會把該值壓進cache里面,并返回。假設cache既不存在該值也不能通過create創建一個新值,那么此方法返回null。
⑤、在緩存中加入一個鍵值對:
將一個鍵值對加入進緩存,注意key和value均不能為null,此方法假設成功將鍵值對加入進緩存,那么就會返回null。
假設返回的值不為null。說明緩存里面已經存在了該key相應的值,不能再進行加入。
⑥、將某個對象從緩存區中移除:
⑦、計算某個實體占用的內存空間:
此方法非常重要此方法特別重要。這種方法默認的實現是每次有一個實體加入至緩存。就+1,導致興許計算緩存是否足夠容納實體的時候,是通過推斷實體個數來計算。而不是依據實體占用的空間來比較。所以一般來說。這種方法都須要重寫,此方法應該返回當前key相應的實體占用的空間大小。
⑧、清除緩存空間占用的全部對象:
此方法會依次將全部存儲的對象移除。
⑨、返回緩存中的全部對象:
會返回依照近期最少使用排序的LinkedHashMap對象。此對象保存了全部的緩存對象。樣例在后面給出,如今先總的介紹要用到的知識。
二、DiskLruCache
DiskLruCache并非android提供的api,而是一個開源庫的代碼,這個類受到了Google的強烈推薦。所以。這里將介紹它。它事實上是一個使用文件系統的採用近期最少用算法的緩存類。DiskLruCache是一個擁有一定空間的文件系統方式的緩存對象,當中的每一個緩存實體都包括有一個字符串類型的key,和固定數量的文件(一個key能夠相應多個文件。這些文件都保存著緩存數據)。這些文件是按順序排列的,通常來說是一些文件或者輸入流的字節形式的。每一個文件得長度都必須介于0-Integer.MAXVALUE之間。
DiskLruCache是利用文件系統的文件夾來存儲數據的,緩存對象會經常對此文件夾進行刪除文件獲取覆蓋文件的操作。因此該文件夾必須是該DiskLruCache專用的。當我們進行DiskLruCache對象創建的時候,應該指定一個緩存大小給它,當緩存數據的大小超過限制的大小的時候。它會在后臺將一些緩存實體刪除掉,直到緩存的大小達到限制的值。同一時候要注意的一點時。這個限制值不是絕對的,比方DiskLruCache進行文件刪除的時候,緩存的容量可能會臨時的超越限制的值。
當想要更新或者創建一個緩存實體的話,應該調用DiskLruCache的edit方法獲取一個Editor對象。此對象假設是null表示當前的值不可編輯。此方法必須和Editor.commit或者Editor.abort相應。另外就是,使用這個類進行緩存的時候,應該捕獲一些常見的I\O操作異常,由于這個類在進行寫文件的時候假設發現錯誤。僅僅會改動失敗,并不會導致操作上的失敗。興許提到的緩存對象指的是DiskLruCache。緩存實體指的是保存在緩存對象里面的具體對象。
①、DiskLruCache對象的獲取:
參數解析:
directory:表示存儲緩存數據的文件。
appVersion:當前應用的appVersion。主要作用在于書寫緩存日志的時候,用來標識緩存數據的版本號號。
valueCount:每一個緩存實體能夠相應的文件的個數。key->多個緩存數據
maxSoze:DiskLruCache最大能夠緩存的數據容量大小。
②、獲取緩存實體。為了獲取緩存實體,先要了解一下Snopshot,這是一個DiskLruCache的內部類。它代表了一個緩存實體的值。關于這個類,有兩個重要的經常用法:例如以下:
前面提到過一個key是能夠相應多個緩存文件的,這里的index就是指該key所相應的第index文件。能夠通過第一種方法獲得該文件的輸入流讀取內容。假設該文件保存的是字符串的內容,能夠通過另外一種方法直接獲取到字符串值。
另外,讀取完文件的內容后。須要調用close方法將給key相應的緩存文件關閉。
那么怎樣獲取某個key相應的緩存實體的Snopshot對象內。例如以下:
③、改動,加入緩存內容。
相同的。我們須要先了解Editor這個類。這也是DiskLruCache提供的內部類。
這個類代表了某個可編輯的緩存實體。這個類也提供了兩個方法進行讀取緩存數據的內容。例如以下:
以及
和前面一樣。第一個方法獲取的是文件的輸入流。第二個方法獲取的是文件的內容轉換為字符串之后的值。上述兩個方法用于讀取文件內容。假設是要進行編輯內容的話。須要調用例如以下方法:
用于獲取某個key相應下標的文件輸出流,興許我們須要把緩存的內容通過它寫入緩存。調用這個之后。必須調用commit或者abort方法。來確認是否改動或者覆蓋緩存內容。
相同的。要想獲取一個緩存實體的可編輯對象,須要通過例如以下方法:
此方法會返回key相應的可編輯對象,注意。這里即使之前沒有key相關的緩存對象,通過此方法就會在文件系統里新建一個key相關的緩存對象,因此也會返回一個可編輯對象。假設返回的是null,說明這個key相應的可編輯對象正在被調用,當前進程無法調用。
④、刪除某個緩存實體。
⑤、關閉緩存對象:
⑥、刪除全部的緩存數據:
上述兩個類可用于存儲不論什么類型的數據,在這里主要以緩存圖片為樣例給大家解說,所以接下來還須要了解一下BitmapFactory這個類。
三、BitmapFactory
這個類可用于針對各種不同數據源來創建Bitmap對象。
這里面最重要的是Options內部類。以及一個將輸入流轉換成Bitmap的解碼方法。以下具體說明:
Options主要有兩個屬性須要具體了解:
①、inJustDecodeBounds:
假設此值設為true,則用此對象去decode一個bitmap的時候,會返回一個null。可是這并不意味這個inJustDecodeBounds設為true是沒有意義的。由于即使沒有返回bitmap對象,可是解碼后的options對象的其它屬性比方outHeight......仍然會擁有值,這對于興許我們用于推斷bitmap對象的大小是否合適非常實用。并且這樣子做另一個優點就是,它進行解碼圖片的時候不須要去申請內存空間。
②、inSampleSize:
此值代表解壓后的圖片的大小是原來的圖片大小的1/inSampleSize倍。這個大小指包括像素的壓縮和寬度以及高度的壓縮。比方這個值為2,則表示壓縮后的圖片的寬高各自是原來的1/2,像素大小則為1/4。這個值永遠都會是2的倍數,假設賦值了3。那么壓縮的時候。會將2賦值給它,即偏小的靠近2的倍數的那個數。
BitmapFactory能夠將非常多不同格式存儲的數據源解碼成Bitmap對象,常見的比方decodeFile,decodeResource,DecodeStream,DecodeByteArrays。此類包括的重載方法太多。就不一一解釋了,這里主要解說decodeStream方法,它是最可能用的到,上面提到的前三個方法以及它們的重載方法。最后都是通過調用decodeStream方法進行解碼的。 方法原型例如以下:
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)
參數解析:
is:用于解析bitmap的原始數據。
ourPadding:解碼后的圖片距離邊界的間隙,假設設為null,表示間隙為0。
opts:解碼的解碼規則。bitmap解碼會依據opts里面的屬性進行解碼。
此方法會返回一個bitmap對象。當然假設is數據無法進行解碼,就會返回null,但假設opts里面的inJustDecodeBounds方法是true的話,依舊會返回null。可是此時的opts的outWidth,outHeight是有值的。
以下通過寫一個樣例來綜合應用如上所說的知識,樣例是書寫一個利用三級緩存讀取網絡圖片的樣例,這個樣例對讀取的圖片未採取壓縮,而是應用原來的圖片,假設有興趣研究的話,讀者自行加入壓縮圖片的代碼,另外說明,運用LruCache最好使用support.v4支持包的,以便于兼容3.1一下的版本號。
DiskLruCache的源代碼例如以下(亦能夠依據前面給的鏈接自行復制):
/** Copyright (C) 2011 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.cw.cache;import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.Closeable; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;/********************************************************************************* Taken from the JB source code, can be found in:* libcore/luni/src/main/java/libcore/io/DiskLruCache.java* or direct link:* https://android.googlesource.com/platform/libcore/+/android-4.1.1_r1/luni/src/main/java/libcore/io/DiskLruCache.java******************************************************************************** A cache that uses a bounded amount of space on a filesystem. Each cache* entry has a string key and a fixed number of values. Values are byte* sequences, accessible as streams or files. Each value must be between {@code* 0} and {@code Integer.MAX_VALUE} bytes in length.** <p>The cache stores its data in a directory on the filesystem. This* directory must be exclusive to the cache; the cache may delete or overwrite* files from its directory. It is an error for multiple processes to use the* same cache directory at the same time.** <p>This cache limits the number of bytes that it will store on the* filesystem. When the number of stored bytes exceeds the limit, the cache will* remove entries in the background until the limit is satisfied. The limit is* not strict: the cache may temporarily exceed it while waiting for files to be* deleted. The limit does not include filesystem overhead or the cache* journal so space-sensitive applications should set a conservative limit.** <p>Clients call {@link #edit} to create or update the values of an entry. An* entry may have only one editor at one time; if a value is not available to be* edited then {@link #edit} will return null.* <ul>* <li>When an entry is being <strong>created</strong> it is necessary to* supply a full set of values; the empty value should be used as a* placeholder if necessary.* <li>When an entry is being <strong>edited</strong>, it is not necessary* to supply data for every value; values default to their previous* value.* </ul>* Every {@link #edit} call must be matched by a call to {@link Editor#commit}* or {@link Editor#abort}. Committing is atomic: a read observes the full set* of values as they were before or after the commit, but never a mix of values.** <p>Clients call {@link #get} to read a snapshot of an entry. The read will* observe the value at the time that {@link #get} was called. Updates and* removals after the call do not impact ongoing reads.** <p>This class is tolerant of some I/O errors. If files are missing from the* filesystem, the corresponding entries will be dropped from the cache. If* an error occurs while writing a cache value, the edit will fail silently.* Callers should handle other problems by catching {@code IOException} and* responding appropriately.*/ public final class DiskLruCache implements Closeable {static final String JOURNAL_FILE = "journal";static final String JOURNAL_FILE_TMP = "journal.tmp";static final String MAGIC = "libcore.io.DiskLruCache";static final String VERSION_1 = "1";static final long ANY_SEQUENCE_NUMBER = -1;private static final String CLEAN = "CLEAN";private static final String DIRTY = "DIRTY";private static final String REMOVE = "REMOVE";private static final String READ = "READ";private static final Charset UTF_8 = Charset.forName("UTF-8");private static final int IO_BUFFER_SIZE = 8 * 1024;/** This cache uses a journal file named "journal". A typical journal file* looks like this:* libcore.io.DiskLruCache* 1* 100* 2** CLEAN 3400330d1dfc7f3f7f4b8d4d803dfcf6 832 21054* DIRTY 335c4c6028171cfddfbaae1a9c313c52* CLEAN 335c4c6028171cfddfbaae1a9c313c52 3934 2342* REMOVE 335c4c6028171cfddfbaae1a9c313c52* DIRTY 1ab96a171faeeee38496d8b330771a7a* CLEAN 1ab96a171faeeee38496d8b330771a7a 1600 234* READ 335c4c6028171cfddfbaae1a9c313c52* READ 3400330d1dfc7f3f7f4b8d4d803dfcf6** The first five lines of the journal form its header. They are the* constant string "libcore.io.DiskLruCache", the disk cache's version,* the application's version, the value count, and a blank line.** Each of the subsequent lines in the file is a record of the state of a* cache entry. Each line contains space-separated values: a state, a key,* and optional state-specific values.* o DIRTY lines track that an entry is actively being created or updated.* Every successful DIRTY action should be followed by a CLEAN or REMOVE* action. DIRTY lines without a matching CLEAN or REMOVE indicate that* temporary files may need to be deleted.* o CLEAN lines track a cache entry that has been successfully published* and may be read. A publish line is followed by the lengths of each of* its values.* o READ lines track accesses for LRU.* o REMOVE lines track entries that have been deleted.** The journal file is appended to as cache operations occur. The journal may* occasionally be compacted by dropping redundant lines. A temporary file named* "journal.tmp" will be used during compaction; that file should be deleted if* it exists when the cache is opened.*/private final File directory;private final File journalFile;private final File journalFileTmp;private final int appVersion;private final long maxSize;private final int valueCount;private long size = 0;private Writer journalWriter;private final LinkedHashMap<String, Entry> lruEntries= new LinkedHashMap<String, Entry>(0, 0.75f, true);private int redundantOpCount;/*** To differentiate between old and current snapshots, each entry is given* a sequence number each time an edit is committed. A snapshot is stale if* its sequence number is not equal to its entry's sequence number.*/private long nextSequenceNumber = 0;/* From java.util.Arrays */@SuppressWarnings("unchecked")private static <T> T[] copyOfRange(T[] original, int start, int end) {final int originalLength = original.length; // For exception priority compatibility.if (start > end) {throw new IllegalArgumentException();}if (start < 0 || start > originalLength) {throw new ArrayIndexOutOfBoundsException();}final int resultLength = end - start;final int copyLength = Math.min(resultLength, originalLength - start);final T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength);System.arraycopy(original, start, result, 0, copyLength);return result;}/*** Returns the remainder of 'reader' as a string, closing it when done.*/public static String readFully(Reader reader) throws IOException {try {StringWriter writer = new StringWriter();char[] buffer = new char[1024];int count;while ((count = reader.read(buffer)) != -1) {writer.write(buffer, 0, count);}return writer.toString();} finally {reader.close();}}/*** Returns the ASCII characters up to but not including the next "\r\n", or* "\n".** @throws EOFException if the stream is exhausted before the next newline* character.*/public static String readAsciiLine(InputStream in) throws IOException {// TODO: support UTF-8 here insteadStringBuilder result = new StringBuilder(80);while (true) {int c = in.read();if (c == -1) {throw new EOFException();} else if (c == '\n') {break;}result.append((char) c);}int length = result.length();if (length > 0 && result.charAt(length - 1) == '\r') {result.setLength(length - 1);}return result.toString();}/*** Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.*/public static void closeQuietly(Closeable closeable) {if (closeable != null) {try {closeable.close();} catch (RuntimeException rethrown) {throw rethrown;} catch (Exception ignored) {}}}/*** Recursively delete everything in {@code dir}.*/// TODO: this should specify paths as Strings rather than as Filespublic static void deleteContents(File dir) throws IOException {File[] files = dir.listFiles();if (files == null) {throw new IllegalArgumentException("not a directory: " + dir);}for (File file : files) {if (file.isDirectory()) {deleteContents(file);}if (!file.delete()) {throw new IOException("failed to delete file: " + file);}}}/** This cache uses a single background thread to evict entries. */private final ExecutorService executorService = new ThreadPoolExecutor(0, 1,60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());private final Callable<Void> cleanupCallable = new Callable<Void>() {@Override public Void call() throws Exception {synchronized (DiskLruCache.this) {if (journalWriter == null) {return null; // closed}trimToSize();if (journalRebuildRequired()) {rebuildJournal();redundantOpCount = 0;}}return null;}};private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) {this.directory = directory;this.appVersion = appVersion;this.journalFile = new File(directory, JOURNAL_FILE);this.journalFileTmp = new File(directory, JOURNAL_FILE_TMP);this.valueCount = valueCount;this.maxSize = maxSize;}/*** Opens the cache in {@code directory}, creating a cache if none exists* there.** @param directory a writable directory* @param appVersion* @param valueCount the number of values per cache entry. Must be positive.* @param maxSize the maximum number of bytes this cache should use to store* @throws IOException if reading or writing the cache directory fails*/public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)throws IOException {if (maxSize <= 0) {throw new IllegalArgumentException("maxSize <= 0");}if (valueCount <= 0) {throw new IllegalArgumentException("valueCount <= 0");}// prefer to pick up where we left offDiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);if (cache.journalFile.exists()) {try {cache.readJournal();cache.processJournal();cache.journalWriter = new BufferedWriter(new FileWriter(cache.journalFile, true),IO_BUFFER_SIZE);return cache;} catch (IOException journalIsCorrupt) { // System.logW("DiskLruCache " + directory + " is corrupt: " // + journalIsCorrupt.getMessage() + ", removing");cache.delete();}}// create a new empty cachedirectory.mkdirs();cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);cache.rebuildJournal();return cache;}private void readJournal() throws IOException {InputStream in = new BufferedInputStream(new FileInputStream(journalFile), IO_BUFFER_SIZE);try {String magic = readAsciiLine(in);String version = readAsciiLine(in);String appVersionString = readAsciiLine(in);String valueCountString = readAsciiLine(in);String blank = readAsciiLine(in);if (!MAGIC.equals(magic)|| !VERSION_1.equals(version)|| !Integer.toString(appVersion).equals(appVersionString)|| !Integer.toString(valueCount).equals(valueCountString)|| !"".equals(blank)) {throw new IOException("unexpected journal header: ["+ magic + ", " + version + ", " + valueCountString + ", " + blank + "]");}while (true) {try {readJournalLine(readAsciiLine(in));} catch (EOFException endOfJournal) {break;}}} finally {closeQuietly(in);}}private void readJournalLine(String line) throws IOException {String[] parts = line.split(" ");if (parts.length < 2) {throw new IOException("unexpected journal line: " + line);}String key = parts[1];if (parts[0].equals(REMOVE) && parts.length == 2) {lruEntries.remove(key);return;}Entry entry = lruEntries.get(key);if (entry == null) {entry = new Entry(key);lruEntries.put(key, entry);}if (parts[0].equals(CLEAN) && parts.length == 2 + valueCount) {entry.readable = true;entry.currentEditor = null;entry.setLengths(copyOfRange(parts, 2, parts.length));} else if (parts[0].equals(DIRTY) && parts.length == 2) {entry.currentEditor = new Editor(entry);} else if (parts[0].equals(READ) && parts.length == 2) {// this work was already done by calling lruEntries.get()} else {throw new IOException("unexpected journal line: " + line);}}/*** Computes the initial size and collects garbage as a part of opening the* cache. Dirty entries are assumed to be inconsistent and will be deleted.*/private void processJournal() throws IOException {deleteIfExists(journalFileTmp);for (Iterator<Entry> i = lruEntries.values().iterator(); i.hasNext(); ) {Entry entry = i.next();if (entry.currentEditor == null) {for (int t = 0; t < valueCount; t++) {size += entry.lengths[t];}} else {entry.currentEditor = null;for (int t = 0; t < valueCount; t++) {deleteIfExists(entry.getCleanFile(t));deleteIfExists(entry.getDirtyFile(t));}i.remove();}}}/*** Creates a new journal that omits redundant information. This replaces the* current journal if it exists.*/private synchronized void rebuildJournal() throws IOException {if (journalWriter != null) {journalWriter.close();}Writer writer = new BufferedWriter(new FileWriter(journalFileTmp), IO_BUFFER_SIZE);writer.write(MAGIC);writer.write("\n");writer.write(VERSION_1);writer.write("\n");writer.write(Integer.toString(appVersion));writer.write("\n");writer.write(Integer.toString(valueCount));writer.write("\n");writer.write("\n");for (Entry entry : lruEntries.values()) {if (entry.currentEditor != null) {writer.write(DIRTY + ' ' + entry.key + '\n');} else {writer.write(CLEAN + ' ' + entry.key + entry.getLengths() + '\n');}}writer.close();journalFileTmp.renameTo(journalFile);journalWriter = new BufferedWriter(new FileWriter(journalFile, true), IO_BUFFER_SIZE);}private static void deleteIfExists(File file) throws IOException { // try { // Libcore.os.remove(file.getPath()); // } catch (ErrnoException errnoException) { // if (errnoException.errno != OsConstants.ENOENT) { // throw errnoException.rethrowAsIOException(); // } // }if (file.exists() && !file.delete()) {throw new IOException();}}/*** Returns a snapshot of the entry named {@code key}, or null if it doesn't* exist is not currently readable. If a value is returned, it is moved to* the head of the LRU queue.*/public synchronized Snapshot get(String key) throws IOException {checkNotClosed();validateKey(key);Entry entry = lruEntries.get(key);if (entry == null) {return null;}if (!entry.readable) {return null;}/** Open all streams eagerly to guarantee that we see a single published* snapshot. If we opened streams lazily then the streams could come* from different edits.*/InputStream[] ins = new InputStream[valueCount];try {for (int i = 0; i < valueCount; i++) {ins[i] = new FileInputStream(entry.getCleanFile(i));}} catch (FileNotFoundException e) {// a file must have been deleted manually!return null;}redundantOpCount++;journalWriter.append(READ + ' ' + key + '\n');if (journalRebuildRequired()) {executorService.submit(cleanupCallable);}return new Snapshot(key, entry.sequenceNumber, ins);}/*** Returns an editor for the entry named {@code key}, or null if another* edit is in progress.*/public Editor edit(String key) throws IOException {return edit(key, ANY_SEQUENCE_NUMBER);}private synchronized Editor edit(String key, long expectedSequenceNumber) throws IOException {checkNotClosed();validateKey(key);Entry entry = lruEntries.get(key);if (expectedSequenceNumber != ANY_SEQUENCE_NUMBER&& (entry == null || entry.sequenceNumber != expectedSequenceNumber)) {return null; // snapshot is stale}if (entry == null) {entry = new Entry(key);lruEntries.put(key, entry);} else if (entry.currentEditor != null) {return null; // another edit is in progress}Editor editor = new Editor(entry);entry.currentEditor = editor;// flush the journal before creating files to prevent file leaksjournalWriter.write(DIRTY + ' ' + key + '\n');journalWriter.flush();return editor;}/*** Returns the directory where this cache stores its data.*/public File getDirectory() {return directory;}/*** Returns the maximum number of bytes that this cache should use to store* its data.*/public long maxSize() {return maxSize;}/*** Returns the number of bytes currently being used to store the values in* this cache. This may be greater than the max size if a background* deletion is pending.*/public synchronized long size() {return size;}private synchronized void completeEdit(Editor editor, boolean success) throws IOException {Entry entry = editor.entry;if (entry.currentEditor != editor) {throw new IllegalStateException();}// if this edit is creating the entry for the first time, every index must have a valueif (success && !entry.readable) {for (int i = 0; i < valueCount; i++) {if (!entry.getDirtyFile(i).exists()) {editor.abort();throw new IllegalStateException("edit didn't create file " + i);}}}for (int i = 0; i < valueCount; i++) {File dirty = entry.getDirtyFile(i);if (success) {if (dirty.exists()) {File clean = entry.getCleanFile(i);dirty.renameTo(clean);long oldLength = entry.lengths[i];long newLength = clean.length();entry.lengths[i] = newLength;size = size - oldLength + newLength;}} else {deleteIfExists(dirty);}}redundantOpCount++;entry.currentEditor = null;if (entry.readable | success) {entry.readable = true;journalWriter.write(CLEAN + ' ' + entry.key + entry.getLengths() + '\n');if (success) {entry.sequenceNumber = nextSequenceNumber++;}} else {lruEntries.remove(entry.key);journalWriter.write(REMOVE + ' ' + entry.key + '\n');}if (size > maxSize || journalRebuildRequired()) {executorService.submit(cleanupCallable);}}/*** We only rebuild the journal when it will halve the size of the journal* and eliminate at least 2000 ops.*/private boolean journalRebuildRequired() {final int REDUNDANT_OP_COMPACT_THRESHOLD = 2000;return redundantOpCount >= REDUNDANT_OP_COMPACT_THRESHOLD&& redundantOpCount >= lruEntries.size();}/*** Drops the entry for {@code key} if it exists and can be removed. Entries* actively being edited cannot be removed.** @return true if an entry was removed.*/public synchronized boolean remove(String key) throws IOException {checkNotClosed();validateKey(key);Entry entry = lruEntries.get(key);if (entry == null || entry.currentEditor != null) {return false;}for (int i = 0; i < valueCount; i++) {File file = entry.getCleanFile(i);if (!file.delete()) {throw new IOException("failed to delete " + file);}size -= entry.lengths[i];entry.lengths[i] = 0;}redundantOpCount++;journalWriter.append(REMOVE + ' ' + key + '\n');lruEntries.remove(key);if (journalRebuildRequired()) {executorService.submit(cleanupCallable);}return true;}/*** Returns true if this cache has been closed.*/public boolean isClosed() {return journalWriter == null;}private void checkNotClosed() {if (journalWriter == null) {throw new IllegalStateException("cache is closed");}}/*** Force buffered operations to the filesystem.*/public synchronized void flush() throws IOException {checkNotClosed();trimToSize();journalWriter.flush();}/*** Closes this cache. Stored values will remain on the filesystem.*/public synchronized void close() throws IOException {if (journalWriter == null) {return; // already closed}for (Entry entry : new ArrayList<Entry>(lruEntries.values())) {if (entry.currentEditor != null) {entry.currentEditor.abort();}}trimToSize();journalWriter.close();journalWriter = null;}private void trimToSize() throws IOException {while (size > maxSize) { // Map.Entry<String, Entry> toEvict = lruEntries.eldest();final Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next();remove(toEvict.getKey());}}/*** Closes the cache and deletes all of its stored values. This will delete* all files in the cache directory including files that weren't created by* the cache.*/public void delete() throws IOException {close();deleteContents(directory);}private void validateKey(String key) {if (key.contains(" ") || key.contains("\n") || key.contains("\r")) {throw new IllegalArgumentException("keys must not contain spaces or newlines: \"" + key + "\"");}}private static String inputStreamToString(InputStream in) throws IOException {return readFully(new InputStreamReader(in, UTF_8));}/*** A snapshot of the values for an entry.*/public final class Snapshot implements Closeable {private final String key;private final long sequenceNumber;private final InputStream[] ins;private Snapshot(String key, long sequenceNumber, InputStream[] ins) {this.key = key;this.sequenceNumber = sequenceNumber;this.ins = ins;}/*** Returns an editor for this snapshot's entry, or null if either the* entry has changed since this snapshot was created or if another edit* is in progress.*/public Editor edit() throws IOException {return DiskLruCache.this.edit(key, sequenceNumber);}/*** Returns the unbuffered stream with the value for {@code index}.*/public InputStream getInputStream(int index) {return ins[index];}/*** Returns the string value for {@code index}.*/public String getString(int index) throws IOException {return inputStreamToString(getInputStream(index));}@Override public void close() {for (InputStream in : ins) {closeQuietly(in);}}}/*** Edits the values for an entry.*/public final class Editor {private final Entry entry;private boolean hasErrors;private Editor(Entry entry) {this.entry = entry;}/*** Returns an unbuffered input stream to read the last committed value,* or null if no value has been committed.*/public InputStream newInputStream(int index) throws IOException {synchronized (DiskLruCache.this) {if (entry.currentEditor != this) {throw new IllegalStateException();}if (!entry.readable) {return null;}return new FileInputStream(entry.getCleanFile(index));}}/*** Returns the last committed value as a string, or null if no value* has been committed.*/public String getString(int index) throws IOException {InputStream in = newInputStream(index);return in != null ?
inputStreamToString(in) : null; } /** * Returns a new unbuffered output stream to write the value at * {@code index}. If the underlying output stream encounters errors * when writing to the filesystem, this edit will be aborted when * {@link #commit} is called. The returned output stream does not throw * IOExceptions. */ public OutputStream newOutputStream(int index) throws IOException { synchronized (DiskLruCache.this) { if (entry.currentEditor != this) { throw new IllegalStateException(); } return new FaultHidingOutputStream(new FileOutputStream(entry.getDirtyFile(index))); } } /** * Sets the value at {@code index} to {@code value}. */ public void set(int index, String value) throws IOException { Writer writer = null; try { writer = new OutputStreamWriter(newOutputStream(index), UTF_8); writer.write(value); } finally { closeQuietly(writer); } } /** * Commits this edit so it is visible to readers. This releases the * edit lock so another edit may be started on the same key. */ public void commit() throws IOException { if (hasErrors) { completeEdit(this, false); remove(entry.key); // the previous entry is stale } else { completeEdit(this, true); } } /** * Aborts this edit. This releases the edit lock so another edit may be * started on the same key. */ public void abort() throws IOException { completeEdit(this, false); } private class FaultHidingOutputStream extends FilterOutputStream { private FaultHidingOutputStream(OutputStream out) { super(out); } @Override public void write(int oneByte) { try { out.write(oneByte); } catch (IOException e) { hasErrors = true; } } @Override public void write(byte[] buffer, int offset, int length) { try { out.write(buffer, offset, length); } catch (IOException e) { hasErrors = true; } } @Override public void close() { try { out.close(); } catch (IOException e) { hasErrors = true; } } @Override public void flush() { try { out.flush(); } catch (IOException e) { hasErrors = true; } } } } private final class Entry { private final String key; /** Lengths of this entry's files. */ private final long[] lengths; /** True if this entry has ever been published */ private boolean readable; /** The ongoing edit or null if this entry is not being edited. */ private Editor currentEditor; /** The sequence number of the most recently committed edit to this entry. */ private long sequenceNumber; private Entry(String key) { this.key = key; this.lengths = new long[valueCount]; } public String getLengths() throws IOException { StringBuilder result = new StringBuilder(); for (long size : lengths) { result.append(' ').append(size); } return result.toString(); } /** * Set lengths using decimal numbers like "10123". */ private void setLengths(String[] strings) throws IOException { if (strings.length != valueCount) { throw invalidLengths(strings); } try { for (int i = 0; i < strings.length; i++) { lengths[i] = Long.parseLong(strings[i]); } } catch (NumberFormatException e) { throw invalidLengths(strings); } } private IOException invalidLengths(String[] strings) throws IOException { throw new IOException("unexpected journal line: " + Arrays.toString(strings)); } public File getCleanFile(int i) { return new File(directory, key + "." + i); } public File getDirtyFile(int i) { return new File(directory, key + "." + i + ".tmp"); } } }
首先是image_layout,這是listView的每一個項的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="200dp"android:scaleType="fitXY"android:background="#efefef" /> </LinearLayout>
然后是內存緩存的實現類。MemoryCache:
package com.cw.cache;import android.graphics.Bitmap; import android.support.v4.util.LruCache;//為了兼容3.1之前的版本號。請使用支持包中的LruCache import android.util.Log;/*** Created by Myy on 2016/7/26.* 內存緩存*/ public class MemoryCache {private static LruCache<String, Bitmap> cache = null;private MemoryCache() {}private static class MemoryCacheHolder {private static MemoryCache cache = new MemoryCache();}public static MemoryCache getInstance() {if (cache == null) {initCache();}return MemoryCacheHolder.cache;}private static void initCache() {int maxMemory = (int) Runtime.getRuntime().maxMemory();Log.i("最大內存", maxMemory + "");cache = new LruCache<String, Bitmap>(maxMemory / 4) {@Overrideprotected int sizeOf(String key, Bitmap value) {Log.i("圖片內存", value.getByteCount() + "");return value.getByteCount();}};}public Bitmap get(String url) {if (url == null || url.length() == 0)throw new NullPointerException("url不可為空");String key = StringUtils.urlToKey(url);if (cache == null)throw new RuntimeException("cache初始化失敗");return cache.get(key);}/*** 緩存一個Bitmap,返回null表示該url相應的值已存在。加入失敗。** @param url* @param bitmap* @return*/public Bitmap put(String url, Bitmap bitmap) {if (url == null || url.length() == 0 || bitmap == null)throw new NullPointerException("url或者圖像不可為空");String key = StringUtils.urlToKey(url);if (cache == null)throw new RuntimeException("cache初始化失敗");return cache.put(key, bitmap);}/*** 清除內存中的全部緩存數據*/public void clearCache() {if (cache == null)throw new RuntimeException("cache初始化失敗");cache.evictAll();} }
接著是文件緩存的實現類:
package com.cw.cache;import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment;import java.io.BufferedInputStream; import java.io.File; import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;/*** Created by Myy on 2016/7/26.* 文件緩存*/ public class DiskCache {private static DiskLruCache cache = null;private static Context c = null;private DiskCache() {}private static class DiskCacheHolder {private static DiskCache diskCache = new DiskCache();}public static DiskCache getInstance(Context context) {if (cache == null) {c = context.getApplicationContext();initCache();}return DiskCacheHolder.diskCache;}private static void initCache() {File fileDirectory = getFileDirectory();//保存緩存文件的文件夾int appVersion = getAppVersion();int valueCount = 1;//這里設置每一個key僅僅相應一個緩存實體int maxSize = 100 * 1024 * 1024;//100MB的緩存空間try {//假設緩存文件夾存在緩存文件則直接使用,否則會在緩存文件夾新建緩存相關的文件cache = DiskLruCache.open(fileDirectory, appVersion, valueCount, maxSize);} catch (IOException e) {e.printStackTrace();}}private static int getAppVersion() {try {return c.getPackageManager().getPackageInfo(c.getPackageName(), 0).versionCode;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}return 1;}private static File getFileDirectory() {String path = null;if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {path = Environment.getExternalStorageDirectory().getPath() + File.separator + "bitmap";} else {path = Environment.getDataDirectory().getPath() + File.separator + "bitmap";}File file = new File(path);if (!file.exists())file.mkdir();return file;}/*** 清除緩存數據*/public void clearCache() {if (cache == null)throw new RuntimeException("初始化失敗");try {cache.delete();} catch (IOException e) {e.printStackTrace();}}/*** 緩存圖片** @param url* @param bitmap*/public void put(String url, Bitmap bitmap) {if (url == null || url.length() == 0 || bitmap == null)throw new NullPointerException("url或者圖像不可為空");String key = StringUtils.urlToKey(url);if (cache == null)throw new RuntimeException("cache初始化失敗");DiskLruCache.Editor editor = null;OutputStream os = null;try {editor = cache.edit(key);//注意后面調用的abort或者commit方法if (editor != null) {os = editor.newOutputStream(0);if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, os)) {throw new RuntimeException("圖片壓縮失敗");}}} catch (Exception e) {e.printStackTrace();try {os.close();editor.abort();} catch (IOException e1) {e1.printStackTrace();}} finally {try {os.close();editor.commit();} catch (IOException e) {e.printStackTrace();}}}/*** 獲取緩存圖片** @param url* @return*/public Bitmap get(String url) {if (url == null || url.length() == 0)throw new NullPointerException("url不可為空");String key = StringUtils.urlToKey(url);if (cache == null)throw new RuntimeException("cache初始化失敗");DiskLruCache.Snapshot snapshot = null;InputStream is = null;Bitmap bitmap = null;try {snapshot = cache.get(key);//返回null表示該值不存在或當前正處于不可讀狀態。if (snapshot != null) {is = snapshot.getInputStream(0);bitmap = BitmapFactory.decodeStream(is);} elsereturn null;} catch (IOException e) {e.printStackTrace();try {snapshot.close();//這里不須要手動關閉is,由于此方法會將剛剛獲取的文件流全部關閉} catch (Exception e1) {e1.printStackTrace();} finally {snapshot.close();}}return bitmap;}}
然后是StringUtils,定義了圖片路徑和圖片路徑轉換成string字符串的方法:
package com.cw.cache;import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;/*** Created by Myy on 2016/7/26.*/ public class StringUtils {public static String urlToKey(String url) {StringBuilder sb = new StringBuilder();try {MessageDigest digest = MessageDigest.getInstance("MD5");digest.update(url.getBytes());byte[] bytes = digest.digest();for (int i = 0; i < bytes.length; i++)sb.append(Integer.toHexString(0xff & bytes[i]));} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return sb.toString().length() == 0 ? null : sb.toString();}public static String[] urlS = new String[]{"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimage53.360doc.com%2FDownloadImg%2F2012%2F07%2F2317%2F25701259_6.jpg","http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1308%2F16%2Fc2%2F24549817_1376646910888.jpg","http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fwww.deskcar.com%2Fdesktop%2Ffengjing%2F2013312114415%2F3.jpg","http://image.baidu.com/search/down?
tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2012%2F011%2FR5J8A0HYL5YV.jpg", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fwww.bz55.com%2Fuploads%2Fallimg%2F111017%2F13264160c-25.jpg", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fwww.bz55.com%2Fuploads1%2Fallimg%2F120130%2F1_120130225951_1.jpg", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fwww.bz55.com%2Fuploads%2Fallimg%2F130618%2F1-13061PU440.jpg", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fsoftbbs%2F1008%2F26%2Fc0%2F4984165_1282800005719_1024x1024soft.jpg", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1307%2F10%2Fc3%2F23153824_1373426670894.jpg", "http://img2.imgtn.bdimg.com/it/u=331221080,82593678&fm=206&gp=0.jpg", "http://img4.imgtn.bdimg.com/it/u=562407178,1662987234&fm=206&gp=0.jpg", "http://img3.imgtn.bdimg.com/it/u=2324814778,3433509063&fm=206&gp=0.jpg", "http://img4.imgtn.bdimg.com/it/u=3570507366,2497738850&fm=206&gp=0.jpg", "http://img4.imgtn.bdimg.com/it/u=846199408,2794756692&fm=206&gp=0.jpg", "http://img5.imgtn.bdimg.com/it/u=221456928,362190599&fm=206&gp=0.jpg", "http://img4.imgtn.bdimg.com/it/u=1969253456,2193232238&fm=206&gp=0.jpg", "http://img3.imgtn.bdimg.com/it/u=2695781595,4041188434&fm=206&gp=0.jpg", "http://img0.imgtn.bdimg.com/it/u=3662196526,1418421672&fm=206&gp=0.jpg", "http://img3.imgtn.bdimg.com/it/u=664997347,4191517248&fm=206&gp=0.jpg", "http://img5.imgtn.bdimg.com/it/u=3641843242,2739246521&fm=206&gp=0.jpg", "http://img3.imgtn.bdimg.com/it/u=2686058747,1067524060&fm=206&gp=0.jpg", "http://img2.imgtn.bdimg.com/it/u=84383536,2556612772&fm=206&gp=0.jpg", "http://img3.imgtn.bdimg.com/it/u=224917259,3388622236&fm=206&gp=0.jpg" }; }
接著是圖片載入器。ImageLoader:
package com.cw.cache;import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Rect; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.widget.ImageView;import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;/*** Created by Myy on 2016/7/26.*/ public class ImageLoader {private static DiskCache diskCache;private static MemoryCache memoryCache;private static ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());private static Handler handler = null;private ImageLoader() {}private static class ImageLoaderHolder {private static ImageLoader imageLoader = new ImageLoader();}public static ImageLoader getInstance(Context context) {if (diskCache == null || memoryCache == null) {diskCache = DiskCache.getInstance(context);memoryCache = MemoryCache.getInstance();handler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {ViewHolder holder = (ViewHolder) msg.obj;Bitmap bitmap = holder.bitmap;ImageView imageView = holder.imageView;if (imageView.getTag().equals(holder.url))imageView.setImageBitmap(bitmap);}};}return ImageLoaderHolder.imageLoader;}/*** 讀取圖片** @param url* @param imageView*/public void getBitmap(String url, ImageView imageView) {Bitmap bitmap = null;imageView.setTag(url);if (diskCache == null || memoryCache == null) {throw new RuntimeException("初始化失敗");} else {//先從內存中讀取。然后文件讀取。最后網絡讀取bitmap = memoryCache.get(url);if (bitmap == null) {bitmap = diskCache.get(url);if (bitmap == null) {getBitmapFromNet(url, imageView);} else {displayBitmap(imageView, bitmap, url);}} else {displayBitmap(imageView, bitmap, url);}}}/*** 從網絡獲取圖片** @param urls*/private synchronized void getBitmapFromNet(final String urls, final ImageView imageView) {pool.execute(new Runnable() {@Overridepublic void run() {try {URL url = new URL(urls);HttpURLConnection con = (HttpURLConnection) url.openConnection();InputStream is = con.getInputStream();Bitmap bitmap = BitmapFactory.decodeStream(is);if (bitmap != null) {memoryCache.put(urls, bitmap);diskCache.put(urls, bitmap);displayBitmap(imageView, bitmap, urls);}} catch (Exception e) {e.printStackTrace();}}});}/*** 顯示圖片** @param imageView* @param bitmap*/private void displayBitmap(ImageView imageView, Bitmap bitmap, String url) {ViewHolder holder = new ViewHolder();holder.bitmap = bitmap;holder.imageView = imageView;holder.url = url;Message msg = Message.obtain(handler, 0);msg.obj = holder;msg.sendToTarget();}private class ViewHolder {Bitmap bitmap;ImageView imageView;String url;}}
然后是圖片適配器,ImageAdapter:
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cw.cache"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--開啟硬件加速有助于渲染圖片--> <application android:allowBackup="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
當我們執行過一遍之后。關掉網絡,再次打開(注意徹底清除任務)假設還能顯示圖片,說明我們的緩存目的達到了。
---------文章寫自:HyHarden---------
--------博客地址:http://blog.csdn.net/qq_25722767-----------
轉載于:https://www.cnblogs.com/llguanli/p/8663776.html
總結
以上是生活随笔為你收集整理的Android中图片的三级缓存策略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记录从数据库把数据初始化mongodb缓
- 下一篇: 基金资金规模大的好吗 需要分析这些方面