我正在使用以下方法
Bitmap bitmap = Picasso.with(ListofCardsActivity.this)
.load(overLayUrl).get();
用于从 web url 下载和获取图像。
这种方法是否每次都从 url 下载图像,即使它已经下载?
我想要的是一旦下载了图像,然后从下一次开始,我应该从缓存中获取图像,无需下载。
如果我们有上述要求的方法。请告诉我
这种方法是否每次都从 url 下载图像,即使它已经下载?
如果它被缓存,则不会。
您返回的Picasso
实例with()
已预先配置为具有内存缓存和磁盘缓存。
根据您下载的数量,您可能会用完缓存空间。如果图像在服务器上发生更改,我希望毕加索使用ETag
和If-Modified-Since
重新下载图像,尽管我没有检查他们的代码以查看它们是否这样做,因为这种行为没有记录。
这种方法是否每次都从 url 下载图像,即使它已经下载?如果它被缓存,则不会。
根据文档和源代码,毕加索在使用同步get()方法时不会缓存任何内容。
所以这是我同步加载图像并用毕加索缓存它的解决方案:
File fileImage = new File("/path/to/your/image");
final Bitmap[] bmpRes = new Bitmap[1];
final Semaphore semaphore = new Semaphore(0);
Picasso.with(this).load(fileImage).priority(Picasso.Priority.HIGH).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmpRes[0] = bitmap;
semaphore.release();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
semaphore.release();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(bmpRes[0] != null) {
Bitmap bmp = bmpRes[0];
//TODO: Whatever you want with the bitmap
} else {
//TODO: Failed to load the image
}