给我一个安卓图片三级缓存的工具类,包括一级缓存网络缓存,二级缓存内存缓存,三级缓存磁盘缓存,并且支持视频序缩略图的查看,异步加载,该程序由java语言编写。
时间: 2023-08-04 07:05:23 浏览: 68
以下是一个简单的安卓图片三级缓存的工具类:
```java
public class ImageLoader {
private static final int MAX_MEMORY_CACHE_SIZE = (int) (Runtime.getRuntime().maxMemory() / 8);
private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024;
private static final int IO_BUFFER_SIZE = 8 * 1024;
private static final String IMAGE_CACHE_DIR = "image_cache";
private static ImageLoader instance;
private LruCache<String, Bitmap> memoryCache;
private DiskLruCache diskCache;
private ImageLoader(Context context) {
initMemoryCache();
initDiskCache(context);
}
public static synchronized ImageLoader getInstance(Context context) {
if (instance == null) {
instance = new ImageLoader(context);
}
return instance;
}
private void initMemoryCache() {
memoryCache = new LruCache<String, Bitmap>(MAX_MEMORY_CACHE_SIZE) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
private void initDiskCache(Context context) {
try {
File cacheDir = getDiskCacheDir(context, IMAGE_CACHE_DIR);
diskCache = DiskLruCache.open(cacheDir, BuildConfig.VERSION_CODE, 1, MAX_DISK_CACHE_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
}
private File getDiskCacheDir(Context context, String uniqueName) {
final String cachePath = context.getCacheDir().getPath();
return new File(cachePath + File.separator + uniqueName);
}
public void displayImage(final ImageView imageView, final String url) {
imageView.setTag(url);
Bitmap bitmap = getBitmapFromMemoryCache(url);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
return;
}
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Bitmap bitmap = getBitmapFromDiskCache(url);
if (bitmap != null) {
addBitmapToMemoryCache(url, bitmap);
if (imageView.getTag().equals(url)) {
imageView.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
}
return;
}
try {
bitmap = getBitmapFromUrl(url);
addBitmapToMemoryCache(url, bitmap);
addBitmapToDiskCache(url, bitmap);
if (imageView.getTag().equals(url)) {
imageView.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private Bitmap getBitmapFromMemoryCache(String key) {
return memoryCache.get(key);
}
private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemoryCache(key) == null) {
memoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromDiskCache(String key) {
try {
DiskLruCache.Snapshot snapshot = diskCache.get(key);
if (snapshot != null) {
InputStream inputStream = snapshot.getInputStream(0);
return BitmapFactory.decodeStream(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void addBitmapToDiskCache(String key, Bitmap bitmap) {
try {
DiskLruCache.Editor editor = diskCache.edit(key);
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(0);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
editor.commit();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Bitmap getBitmapFromUrl(String urlString) throws IOException {
InputStream inputStream = null;
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE);
return BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
```
使用该类加载图片只需调用 `displayImage()` 方法,该方法会先从内存缓存中查找图片,如果找到则直接显示在 ImageView 上,否则会异步去磁盘缓存中查找图片,如果找到则添加到内存缓存,并显示在 ImageView 上,否则会异步去网络上下载图片,下载完成后添加到内存缓存和磁盘缓存,并显示在 ImageView 上。该方法支持传入任何类型的 ImageView,包括 VideoView。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)