ImageLoader is an open source library for image cache, providing a powerful image cache mechanism, which many developers are using. Today, I will introduce to you the local ImageLoader cache for Android development. The specific content is as follows:
There are two ways to modify the file name when cached files are cached locally. Each method corresponds to a Java class
1) HashCodeFileNameGenerator, this class is responsible for obtaining the hashcode of the file name and then converting it into a string.
2) Md5FileNameGenerator, this class saves the name of the source file with md5 encryption.
Both classes inherit the FileNameGenerator interface
A factory method createFileNameGenerator is provided in the DefaultConfigurationFactory class, which returns a default FileNameGenerator object: HashCodeFileNameGenerator.
public static FileNameGenerator createFileNameGenerator() { return new HashCodeFileNameGenerator(); }
accomplish
First, the DiscCacheAware interface is defined, which provides the following methods
File getFileDectory() Return to the root directory of the disk cache File get(String imageUri) according touriGetting pictures from cache boolean save(String imageUri,InputStream iamgeStream, listener) Save the image on disk cache boolean save(String imageUri,Bitmap bitmap) savebitmapObject to disk cache boolean remove(imageUri) according toimageUriDelete files void close() Turn off disk cache,Free up resources void clear() Clear disk cache
Then another methodless interface DiskCache is defined, which simply inherits the DiscCacheAware interface.
BaseDiscCache implements DiskCache, which is an abstract class, which definesThe following properties of the disk buffer:
1) The default cache size is 32k
2) The default compressed image format is PNG (as the first parameter of Bitmap's compress method)
3) The quality of the image after compression is 100, that is, the compression rate is 0, and no compression is performed (as the second parameter of compress)
Provides set methods to modify the compressed image format and compression rate and modify the cache size. At the same time, the class also encapsulates the following three properties
protected final File cacheDir;//Save the cache file Directoryprotected final File reserveCacheDir;//Diectory of backup cache, when cacheDir does not exist, reserveCahceDir backup cache is used to use reserveCahceDir backup cache when cacheDir does not existprotected final FileNameGenerator fileNameGenerator;//File name generator
Constructor
public BaseDiscCache(File cacheDir) { this(cacheDir, null); } public BaseDiscCache(File cacheDir, File reserveCacheDir) { this(cacheDir, reserveCacheDir, ()); } public BaseDiscCache(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); } = cacheDir; = reserveCacheDir; = fileNameGenerator; }
1) The constructor with only one parameter only initializes cacheDir, does not use the fallback cache, and uses HashCodeFileNameGenerator to generate the file name of the target file.
2) In addition to cacheDir and HashCodefileNameGenerator, the two-parameter constructor can also initialize the backup cache
3) The constructor of the three parameters requires that cacheDir must be initialized and filenNameGenerator must be initialized otherwise an exception will be reported.
get(String imageUri)
protected File getFile(String imageUri) { String fileName = (imageUri); File dir = cacheDir; if (!() && !()) { if (reserveCacheDir != null && (() || ())) { dir = reserveCacheDir; } } return new File(dir, fileName); }
save(String imageUri, Bitmap bitmap)
public boolean save(String imageUri, Bitmap bitmap) throws IOException { //Get imageUri File object, which encapsulates the cache path and the name after image savingFile imageFile = getFile(imageUri); //Get the tmpFile object that temporarily saves the fileFile tmpFile = new File(() + TEMP_IMAGE_POSTFIX); OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize); boolean savedSuccessfully = false; try { //Call compress the bitMap into tempFilesavedSuccessfully = (compressFormat, compressQuality, os); } finally { (os); //If the save is successful and the tempFile file is not successfully moved to the imageFile, delete the tempFileif (savedSuccessfully && !(imageFile)) { savedSuccessfully = false; } if (!savedSuccessfully) { (); } } //Garbage collection of bitmap(); return savedSuccessfully; }
BaseDiscCache has two extension classes, one is UnlimitedDiscCache that does not limit the cache size and LimitedAgeDiscCache that limits the cache time. UnlimitedDiscCache is very simple. It simply inherits BaseDiscCache and does not make any extensions to BaseDiscCache.
LimitedAgeDiscCache This class implements deleting files loaded in the cache for more than the specified time: Delete files from the cache when the following conditions are met: System current time - the latest modification time of the file > maxFileAge
LimitedAgeDiscCache
This class provides two properties:
1. maxFileAge (long) sets the maximum timeout for loading. The time is initialized by the constructor. Once initialized, it cannot be changed (set the maximum time for file to survive. When this value exceeds, delete the file)
2. loadingDates (Map<File,long>), this property is a map type object. The key saves the image file to be cached. The value saves the call to save method is the current time of the system. The specific filling of data to loadingDates is implemented in the rememberUsage method below. This method is called in the two save methods in the class. First, the save method of the parent class is called, and then this method is called.
private void rememberUsage(String imageUri) { File file = getFile(imageUri); long currentTime = (); (currentTime); (file, currentTime); }
The method to get data from the cache is get(String imageUri). This class is to override the BaseDiscDache method. This method obtains the latest update time of the image represented by imageUri from loadingDates, and then uses the current time and loadingDate as a difference. If the difference is greater than maxFileAge, that is, check the maximum loading time, delete the file represented by imageUri and the data in loadingDates. Of course, if there is no imageUri in the map, there will be no timeout issue. At this time, put the image into the map. The specific implementation is as follows
@Override public File get(String imageUri) { File file = (imageUri); if (file != null && ()) { boolean cached; Long loadingDate = (file); if (loadingDate == null) { cached = false; loadingDate = (); } else { cached = true; } if (() - loadingDate > maxFileAge) { (); (file); } else if (!cached) { (file, loadingDate); } } return file; }