现在流行的图片加载缓存类库有glide、picasso、ImageLoader
现在有个需求,就是app中要求点击图片放大功能,为了简便我们准备调用系统图片查看器,但是系统图片查看器是查看本地文件的,所以会需要把图片下载到本地来
然后就出现一个问题,我使用了类库后,想直接加载磁盘缓存,这样就可以节省流量,在ImageLoader中,可以通过getDiskCache().get(url)获取到磁盘缓存,然后就可以另存为或者直接加载图片查看器,但是glide和picasso没有找到类似的方法,有谁知道如何获取吗?
#Glide
Glide.with(mContext).load("").downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).get();
参考这篇文章
#Picasso
通过以下方式可以获取到缓存文件的输入流
HttpResponseCache cache = HttpResponseCache.getInstalled();
try {
CacheResponse res = cache.get(URI.create(url), "GET", null);
Bitmap bitmap = BitmapFactory.decodeStream(res.getBody());
ivTest.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
在官方github的issues1497找到了相关的回答,picasso没有磁盘缓存,他将磁盘缓存委托给了底层http,所以picasso无法通过任何schema获取到对应的已缓存的图片路径,但是可以通过上面的代码,或者通过into(target)的方式获取到Bitmap对象,然后我们自己写代码将bitmap放到本地存储空间里
#Glide
Glide.with(mContext).load("").downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).get();
参考这篇文章
#Picasso
通过以下方式可以获取到缓存文件的输入流
HttpResponseCache cache = HttpResponseCache.getInstalled();
try {
CacheResponse res = cache.get(URI.create(url), "GET", null);
Bitmap bitmap = BitmapFactory.decodeStream(res.getBody());
ivTest.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
在官方github的issues1497找到了相关的回答,picasso没有磁盘缓存,他将磁盘缓存委托给了底层http,所以picasso无法通过任何schema获取到对应的已缓存的图片路径,但是可以通过上面的代码,或者通过into(target)的方式获取到Bitmap对象,然后我们自己写代码将bitmap放到本地存储空间里
你有找到解决方案吗,我也遇到这个问题
DiskLruCacheFactory 类里有一个 build() 方法,会返回一个带 (cacheDir(缓存目录), diskCacheSize) 属性的 DiskLruCacheWrapper 对象。
这个是文档,肯定有方法的啊,只是你没找到而已
Nested Class Summary
class DiskLruCache.Editor
Edits the values for an entry.
class DiskLruCache.Value
A snapshot of the values for an entry.
Method Summary
void close()
Closes this cache.
void delete()
Closes the cache and deletes all of its stored values.
DiskLruCache.Editor edit(String key)
Returns an editor for the entry named key, or null if another edit is in progress.
void flush()
Force buffered operations to the filesystem.
DiskLruCache.Value get(String key)
Returns a snapshot of the entry named key, or null if it doesn't exist is not currently readable.
File getDirectory()
Returns the directory where this cache stores its data.
long getMaxSize()
Returns the maximum number of bytes that this cache should use to store its data.
boolean isClosed()
Returns true if this cache has been closed.
static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
Opens the cache in directory, creating a cache if none exists there.
boolean remove(String key)
Drops the entry for key if it exists and can be removed.
void setMaxSize(long maxSize)
Changes the maximum number of bytes the cache can store and queues a job to trim the existing store, if necessary.
long size()
Returns the number of bytes currently being used to store the values in this cache.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Method Detail
open
public static DiskLruCache open(File directory,
int appVersion,
int valueCount,
long maxSize)
throws IOException
Opens the cache in directory, creating a cache if none exists there.
Parameters:
directory - a writable directory
valueCount - the number of values per cache entry. Must be positive.
maxSize - the maximum number of bytes this cache should use to store
Throws:
IOException - if reading or writing the cache directory fails
get
public DiskLruCache.Value get(String key)
throws IOException
Returns a snapshot of the entry named key, or null if it doesn't exist is not currently readable. If a value is returned, it is moved to the head of the LRU queue.
Throws:
IOException
edit
public DiskLruCache.Editor edit(String key)
throws IOException
Returns an editor for the entry named key, or null if another edit is in progress.
Throws:
IOException
getDirectory
public File getDirectory()
Returns the directory where this cache stores its data.
getMaxSize
public long getMaxSize()
Returns the maximum number of bytes that this cache should use to store its data.
setMaxSize
public void setMaxSize(long maxSize)
Changes the maximum number of bytes the cache can store and queues a job to trim the existing store, if necessary.
size
public long size()
Returns the number of bytes currently being used to store the values in this cache. This may be greater than the max size if a background deletion is pending.
remove
public boolean remove(String key)
throws IOException
Drops the entry for key if it exists and can be removed. Entries actively being edited cannot be removed.
Returns:
true if an entry was removed.
Throws:
IOException
isClosed
public boolean isClosed()
Returns true if this cache has been closed.
flush
public void flush()
throws IOException
Force buffered operations to the filesystem.
Throws:
IOException
close
public void close()
throws IOException
Closes this cache. Stored values will remain on the filesystem.
Specified by:
close in interface Closeable
Throws:
IOException
delete
public void delete()
throws IOException
Closes the cache and deletes all of its stored values. This will delete all files in the cache directory including files that weren't created by the cache.
Throws:
IOException
就算解决问题了也不出来说下么