android picasso 缓存目录,使用Picasso和自定义磁盘缓存

如何编写之前,Picasso使用底层Http客户端的缓存。

HttpUrlConnection的内置缓存无法在真正的离线模式下工作, 如果由于某些原因使用OkHttpClient是不可取的 ,则可以使用您自己的磁盘缓存实现(当然基于DiskLruCache )。

其中一种方法是将com.squareup.picasso.UrlConnectionDownloader和programm整个逻辑子类com.squareup.picasso.UrlConnectionDownloader :

@Override public Response load(final Uri uri, int networkPolicy) throws IOException { ... }

然后像这样使用你的实现:

new Picasso.Builder(context).downloader().build();

这是我的UrlConnectionDownloader实现,它与磁盘缓存一起使用,即使在完全脱机模式下也可以发送到Picasso位图:

public class PicassoBitmapDownloader extends UrlConnectionDownloader { private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB @NonNull private Context context; @Nullable private DiskLruCache diskCache; public class IfModifiedResponse extends Response { private final String ifModifiedSinceDate; public IfModifiedResponse(InputStream stream, boolean loadedFromCache, long contentLength, String ifModifiedSinceDate) { super(stream, loadedFromCache, contentLength); this.ifModifiedSinceDate = ifModifiedSinceDate; } public String getIfModifiedSinceDate() { return ifModifiedSinceDate; } } public PicassoBitmapDownloader(@NonNull Context context) { super(context); this.context = context; } @Override public Response load(final Uri uri, int networkPolicy) throws IOException { final String key = getKey(uri); { Response cachedResponse = getCachedBitmap(key); if (cachedResponse != null) { return cachedResponse; } } IfModifiedResponse response = _load(uri); if (cacheBitmap(key, response.getInputStream(), response.getIfModifiedSinceDate())) { IfModifiedResponse cachedResponse = getCachedBitmap(key); if (cachedResponse != null) {return cachedResponse; } } return response; } @NonNull protected IfModifiedResponse _load(Uri uri) throws IOException { HttpURLConnection connection = openConnection(uri); int responseCode = connection.getResponseCode(); if (responseCode >= 300) { connection.disconnect(); throw new ResponseException(responseCode + " " + connection.getResponseMessage(), 0, responseCode); } long contentLength = connection.getHeaderFieldInt("Content-Length", -1); String lastModified = connection.getHeaderField(Constants.HEADER_LAST_MODIFIED); return new IfModifiedResponse(connection.getInputStream(), false, contentLength, lastModified); } @Override protected HttpURLConnection openConnection(Uri path) throws IOException { HttpURLConnection conn = super.openConnection(path); DiskLruCache diskCache = getDiskCache(); DiskLruCache.Snapshot snapshot = diskCache == null ? null : diskCache.get(getKey(path)); if (snapshot != null) { String ifModifiedSince = snapshot.getString(1); if (!isEmpty(ifModifiedSince)) { conn.addRequestProperty(Constants.HEADER_IF_MODIFIED_SINCE, ifModifiedSince); } } return conn; } @Override public void shutdown() { try { if (diskCache != null) { diskCache.flush(); diskCache.close(); } } catch (IOException e) { e.printStackTrace(); } super.shutdown(); } public boolean cacheBitmap(@Nullable String key, @Nullable InputStream inputStream, @Nullable String ifModifiedSince) { if (inputStream == null || isEmpty(key)) { return false; } OutputStream outputStream = null; DiskLruCache.Editor edit = null; try { DiskLruCache diskCache = getDiskCache(); edit = diskCache == null ? null : diskCache.edit(key); outputStream = edit == null ? null : new BufferedOutputStream(edit.newOutputStream(0)); if (outputStream == null) { return false; } ChatUtils.copy(inputStream, outputStream); outputStream.flush(); edit.set(1, ifModifiedSince == null ? "" : ifModifiedSince); edit.commit(); return true; } catch (Exception e) { e.printStackTrace(); } finally { if (edit != null) { edit.abortUnlessCommitted(); } ChatUtils.closeQuietly(outputStream); } return false; } @Nullable public IfModifiedResponse getCachedBitmap(String key) { try { DiskLruCache diskCache = getDiskCache(); DiskLruCache.Snapshot snapshot = diskCache == null ? null : diskCache.get(key); InputStream inputStream = snapshot == null ? null : snapshot.getInputStream(0); if (inputStream == null) { return null; } return new IfModifiedResponse(inputStream, true, snapshot.getLength(0), snapshot.getString(1)); } catch (Exception e) { e.printStackTrace(); } return null; } @Nullable synchronized public DiskLruCache getDiskCache() { if (diskCache == null) { try { File file = new File(context.getCacheDir() + "/images"); if (!file.exists()) { //noinspection ResultOfMethodCallIgnored file.mkdirs(); } long maxSize = calculateDiskCacheSize(file); diskCache = DiskLruCache.open(file, BuildConfig.VERSION_CODE, 2, maxSize); } catch (Exception e) { e.printStackTrace(); } } return diskCache; } @NonNull private String getKey(@NonNull Uri uri) { String key = md5(uri.toString()); return isEmpty(key) ? String.valueOf(uri.hashCode()) : key; } @Nullable public static String md5(final String toEncrypt) { try { final MessageDigest digest = MessageDigest.getInstance("md5"); digest.update(toEncrypt.getBytes()); final byte[] bytes = digest.digest(); final StringBuilder sb = new StringBuilder(); for (byte aByte : bytes) { sb.append(String.format("%02X", aByte)); } return sb.toString().toLowerCase(); } catch (Exception e) { e.printStackTrace(); return null; } } static long calculateDiskCacheSize(File dir) { long available = ChatUtils.bytesAvailable(dir); // Target 2% of the total space. long size = available / 50; // Bound inside min/max size for disk cache. return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值