public class BitmapLruCache extends Object
BitmapLruCache.Builder instance, which can be used to alter the settings of
the resulting cache.
Instances of this class should ideally be kept globally with the application, for example in
the Application object. You should also use the bundled CacheableImageView wherever possible, as the memory cache has a closeStream relationship with it.
Clients can call get(String) to retrieve a cached value from the given Url. This
will check all available caches for the value. There are also the getFromDiskCache(String, android.graphics.BitmapFactory.Options) and getFromMemoryCache(String) which allow more granular access.
There are a number of update methods. put(String, InputStream) and put(String, InputStream) are the preferred versions of the method, as they allow 1:1 caching to
disk of the original content.
put(String, Bitmap)} should only be used if you
can't get access to the original InputStream.
| Modifier and Type | Class and Description |
|---|---|
static class |
BitmapLruCache.Builder
Builder class for {link
BitmapLruCache. |
static class |
BitmapLruCache.RecyclePolicy
The recycle policy controls if the
Bitmap.recycle() is automatically
called, when it is no longer being used. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(String url)
Returns whether any of the enabled caches contain the specified URL.
|
boolean |
containsInDiskCache(String url)
Returns whether the Disk Cache contains the specified URL.
|
boolean |
containsInMemoryCache(String url)
Returns whether the Memory Cache contains the specified URL.
|
CacheableBitmapDrawable |
get(String url)
Returns the value for
url. |
CacheableBitmapDrawable |
get(String url,
android.graphics.BitmapFactory.Options decodeOpts)
Returns the value for
url. |
CacheableBitmapDrawable |
getFromDiskCache(String url,
android.graphics.BitmapFactory.Options decodeOpts)
Returns the value for
url in the disk cache only. |
CacheableBitmapDrawable |
getFromMemoryCache(String url)
Returns the value for
url in the memory cache only. |
boolean |
isDiskCacheEnabled() |
boolean |
isMemoryCacheEnabled() |
CacheableBitmapDrawable |
put(String url,
android.graphics.Bitmap bitmap)
Caches
bitmap for url into all enabled caches. |
CacheableBitmapDrawable |
put(String url,
android.graphics.Bitmap bitmap,
android.graphics.Bitmap.CompressFormat compressFormat,
int compressQuality)
Caches
bitmap for url into all enabled caches. |
CacheableBitmapDrawable |
put(String url,
InputStream inputStream)
Caches resulting bitmap from
inputStream for url into all enabled caches. |
CacheableBitmapDrawable |
put(String url,
InputStream inputStream,
android.graphics.BitmapFactory.Options decodeOpts)
Caches resulting bitmap from
inputStream for url into all enabled caches. |
void |
remove(String url)
Removes the entry for
url from all enabled caches, if it exists. |
void |
trimMemory()
This method iterates through the memory cache (if enabled) and removes any entries which are
not currently being displayed.
|
public boolean contains(String url)
url - the URL to search for.true if any of the caches contain the specified URL, false
otherwise.public boolean containsInDiskCache(String url)
url - the URL to search for.true if the Disk Cache is enabled and contains the specified URL, false otherwise.public boolean containsInMemoryCache(String url)
url - the URL to search for.true if the Memory Cache is enabled and contains the specified URL, false otherwise.public CacheableBitmapDrawable get(String url)
url. This will check all caches currently enabled. If you
have the disk cache enabled, you should not call this method from main/UI thread.url - - String representing the URL of the imagepublic CacheableBitmapDrawable get(String url, android.graphics.BitmapFactory.Options decodeOpts)
url. This will check all caches currently enabled. If you
have the disk cache enabled, you should not call this method from main/UI thread.url - - String representing the URL of the imagedecodeOpts - - Options used for decoding the contents from the disk cache only.public CacheableBitmapDrawable getFromDiskCache(String url, android.graphics.BitmapFactory.Options decodeOpts)
url in the disk cache only. You should not call this method
from main/UI thread. If enabled, the result of this method will be cached in the memory
cache. Unless you have a specific requirement to only query the disk cache, you should
call get(String) instead.url - - String representing the URL of the imagedecodeOpts - - Options used for decoding the contents from the disk cache.url from disk cache, or null if the disk cache is not
enabled.public CacheableBitmapDrawable getFromMemoryCache(String url)
url in the memory cache only. This method is safe to be called
from the main thread. You should check the result of this method before starting a
threaded call.url - - String representing the URL of the imageurl from memory cache, or null if the disk cache is not
enabled.public boolean isDiskCacheEnabled()
public boolean isMemoryCacheEnabled()
public CacheableBitmapDrawable put(String url, android.graphics.Bitmap bitmap)
bitmap for url into all enabled caches. If the disk cache is enabled,
the bitmap will be compressed losslessly. If you have the disk cache enabled, you should
not call this method from main/UI thread.url - - String representing the URL of the image.bitmap - - Bitmap which has been decoded from url.public CacheableBitmapDrawable put(String url, android.graphics.Bitmap bitmap, android.graphics.Bitmap.CompressFormat compressFormat, int compressQuality)
bitmap for url into all enabled caches. If the disk cache is enabled,
the bitmap will be compressed with the settings you provide.
If you have the disk cache enabled, you should not call this method from main/UI thread.url - - String representing the URL of the image.bitmap - - Bitmap which has been decoded from url.compressFormat - - Compression Format to usecompressQuality - - Level of compression to useBitmap.compress(Bitmap.CompressFormat, int, OutputStream)public CacheableBitmapDrawable put(String url, InputStream inputStream)
inputStream for url into all enabled caches.
This version of the method should be preferred as it allows the original image contents to be
cached, rather than a re-compressed version. The contents of the InputStream will be
copied to a temporary file, then the file will be decoded into a Bitmap. Providing the decode
worked: url - - String representing the URL of the imageinputStream - - InputStream opened from urlpublic CacheableBitmapDrawable put(String url, InputStream inputStream, android.graphics.BitmapFactory.Options decodeOpts)
inputStream for url into all enabled caches.
This version of the method should be preferred as it allows the original image contents to be
cached, rather than a re-compressed version. The contents of the InputStream will be
copied to a temporary file, then the file will be decoded into a Bitmap, using the optional
decodeOpts. Providing the decode worked: url - - String representing the URL of the imageinputStream - - InputStream opened from urldecodeOpts - - Options used for decoding. This does not affect what is cached in the
disk cache (if enabled).public void remove(String url)
url from all enabled caches, if it exists. If you have the
disk cache enabled, you should not call this method from main/UI thread.public void trimMemory()
Application.onLowMemory().Copyright © 2013. All Rights Reserved.