概述
我要說的就是鼎鼎大名的Universal Image Loader,UIL是非常強大的一款圖片加載框架,它不僅支持本地圖片加載也支持網絡圖片加載還支持Android自身的drawable文件夾,asset文件夾裏面的圖片文件加載,也支持視頻文件的縮略圖加載。可以說是一款非常全面而強大的圖片加載框架。說到這裏你是不是也對這個框架非常好奇呢?
首先我們來看看UIL的代碼結構。
我們可以看到UIL的代碼結構主要分爲兩大部分,一部分是cache緩存部分,一部分是core主要用於UIL下載,加載和展示圖片功能,當然也暴露了許多定製接口供給開發者使用。
Cache硬盤緩存
現在就來看看cache的硬盤緩存部分的源碼。
大致上硬盤緩存部分有兩種DiskCache,一種是基於BaseDiskCache的LimitedAgeDiskCache和UnlimitedDiskCache,另一種是基於DiskLruCache的LruDiskCache。基於BaseDiskCache的DiskCache沒有使用LRU算法,基於DiskLruCache的有使用到LRU算法,以上的DiskCache都實現了DiskCache接口。
- public interface DiskCache {
- /**
- * Returns root directory of disk cache
- *
- * @return Root directory of disk cache
- */
- File getDirectory();
- /**
- * Returns file of cached image
- *
- * @param imageUri Original image URI
- * @return File of cached image or <b>null</b> if image wasn't cached
- */
- File get(String imageUri);
- /**
- * Saves image stream in disk cache.
- * Incoming image stream shouldn't be closed in this method.
- *
- * @param imageUri Original image URI
- * @param imageStream Input stream of image (shouldn't be closed in this method)
- * @param listener Listener for saving progress, can be ignored if you don't use
- * {@linkplain com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener
- * progress listener} in ImageLoader calls
- * @return <b>true</b> - if image was saved successfully; <b>false</b> - if image wasn't saved in disk cache.
- * @throws java.io.IOException
- */
- boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException;
- /**
- * Saves image bitmap in disk cache.
- *
- * @param imageUri Original image URI
- * @param bitmap Image bitmap
- * @return <b>true</b> - if bitmap was saved successfully; <b>false</b> - if bitmap wasn't saved in disk cache.
- * @throws IOException
- */
- boolean save(String imageUri, Bitmap bitmap) throws IOException;
- /**
- * Removes image file associated with incoming URI
- *
- * @param imageUri Image URI
- * @return <b>true</b> - if image file is deleted successfully; <b>false</b> - if image file doesn't exist for
- * incoming URI or image file can't be deleted.
- */
- boolean remove(String imageUri);
- /** Closes disk cache, releases resources. */
- void close();
- /** Clears disk cache. */
- void clear();
- }
基於BaseDiskCache部分
可以先來看看BaseDiskCache的代碼
- public BaseDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator) {
- if (cacheDir == null) {
- throw new IllegalArgumentException("cacheDir" + ERROR_ARG_NULL);
- }
- if (fileNameGenerator == null) {
- throw new IllegalArgumentException("fileNameGenerator" + ERROR_ARG_NULL);
- }
- this.cacheDir = cacheDir;
- this.reserveCacheDir = reserveCacheDir;
- this.fileNameGenerator = fileNameGenerator;
- }
這是BaseDiskCache的構造方法,裏面使用了cacheDir,reserveCacheDir,fileNameGenerator三個參數,cacheDir是第一緩存路徑,如無意外的話我們就是使用cacheDir作爲緩存路徑,而reserveCacheDir是備用緩存路徑,是在假如第一緩存路徑無法使用的情況下,啓用備用緩存路徑,fileNameGenerator是FileNameGenerator,默認是HashCodeFileNameGenerator,通過文件名的HashCode生成緩存文件名。
- @Override
- public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
- File imageFile = getFile(imageUri);
- File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
- boolean loaded = false;
- try {
- OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
- try {
- loaded = IoUtils.copyStream(imageStream, os, listener, bufferSize);
- } finally {
- IoUtils.closeSilently(os);
- }
- } finally {
- if (loaded && !tmpFile.renameTo(imageFile)) {
- loaded = false;
- }
- if (!loaded) {
- tmpFile.delete();
- }
- }
- return loaded;
- }
該Save方法實現的是將InputStream通過IO流生成到一個.tmp後綴的緩存文件。當然緩存文件還是和uri相關聯。
- @Override
- public boolean remove(String imageUri) {
- return getFile(imageUri).delete();
- }
通過remove方法我們可以看出,通過相關的uri,對其文件刪除,本質上是使用了File的接口。
UnlimitedDiskCache基本上就是繼承於BaseDiskCache,沒有任何改動,所以看看LimitedAgeDiskCache,也是繼承於BaseDiskCache,但是有兩個特有的成員變量。
- private final long maxFileAge;
- private final Map<File, Long> loadingDates = Collections.synchronizedMap(new HashMap<File, Long>());
一個是maxFileAge,用於記錄LimitedAgeDiskCache的日期限制,單位爲秒。還有一個是loadingDates,是一個Map用於記錄File和Date之間的聯繫。
Save方法裏面都用到了rememberUsage(String uri);的方法,事實上就是在Save一個文件的時候,把該文件的修改日期設置上當前時間,並且把文件記錄添加到loadingDates裏面。
- private void rememberUsage(String imageUri) {
- File file = getFile(imageUri);
- long currentTime = System.currentTimeMillis();
- file.setLastModified(currentTime);
- loadingDates.put(file, currentTime);
- }
在通過get(String uri);的方法的時候,對get的File進行檢查,如果超過日期限制就刪除該文件。
- @Override
- public File get(String imageUri) {
- File file = super.get(imageUri);
- if (file != null && file.exists()) {
- boolean cached;
- Long loadingDate = loadingDates.get(file);
- if (loadingDate == null) {
- cached = false;
- loadingDate = file.lastModified();
- } else {
- cached = true;
- }
- if (System.currentTimeMillis() - loadingDate > maxFileAge) {
- file.delete();
- loadingDates.remove(file);
- } else if (!cached) {
- loadingDates.put(file, loadingDate);
- }
- }
- return file;
- }
DiskLruCache簡介
- private void initCache(File cacheDir, File reserveCacheDir, long cacheMaxSize, int cacheMaxFileCount)
- throws IOException {
- try {
- cache = DiskLruCache.open(cacheDir, 1, 1, cacheMaxSize, cacheMaxFileCount);
- } catch (IOException e) {
- L.e(e);
- if (reserveCacheDir != null) {
- initCache(reserveCacheDir, null, cacheMaxSize, cacheMaxFileCount);
- }
- if (cache == null) {
- throw e; //new RuntimeException("Can't initialize disk cache", e);
- }
- }
- }
- @Override
- public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
- DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
- if (editor == null) {
- return false;
- }
- OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
- boolean copied = false;
- try {
- copied = IoUtils.copyStream(imageStream, os, listener, bufferSize);
- } finally {
- IoUtils.closeSilently(os);
- if (copied) {
- editor.commit();
- } else {
- editor.abort();
- }
- }
- return copied;
- }
- private void trimToSize() throws IOException {
- while (size > maxSize) {
- Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next();
- remove(toEvict.getKey());
- }
- }
- private void trimToFileCount() throws IOException {
- while (fileCount > maxFileCount) {
- Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next();
- remove(toEvict.getKey());
- }
- }