Android 使用 fresco 下载后离线显示图像
Android display images when offline once downloaded using fresco
我希望 fresco 在连接到 Internet 时下载图像并将其保存到 SD 卡。
稍后离线时,即使缓存被清除,我仍然需要壁画来显示保存的图像。
这可能吗?如果是,如何?
清除缓存后,简单地将图像保存到磁盘缓存似乎不起作用。
您需要在下载图片时手动将图片保存到磁盘。显示图像时,检查图像是否在磁盘中:如果不是,从 url 下载(并保存到磁盘)。
Fresco 为您缓存图像。如果您处于离线状态,图像仍应显示。你应该不需要做任何事情。
然而,当缓存被清除时(例如,当用户按下按钮或设备 space 电量低时),图像显然会从缓存中删除 - 这是不应更改的预期行为.
有 2 个选项:保存所选项目、移动缓存
保存所选项目
如果你想保留选定的图像(例如"Save"按钮),你可以获取编码图像并将其保存在设备上的某个位置。
您不应对所有图像都执行此操作,因为它们将在磁盘上出现 2 次,并且清除缓存/卸载应用程序会在设备上留下 1 个副本。
类似这样的方法可行:
DataSource<CloseableReference<PooledByteBuffer>>
dataSource = Fresco.getImagePipeline().fetchEncodedImage(imageRequest, callerContext);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
protected void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
CloseableReference<PooledByteBuffer> encodedImage = dataSource.getResult();
if (encodedImage != null) {
try {
// save the encoded image in the PooledByteBuffer
} finally {
CloseableReference.closeSafely(encodedImage);
}
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
// something went wrong
}
}, executorService);
}
有关如何使用管道获取编码图像的更多信息:http://frescolib.org/docs/using-image-pipeline.html
移动缓存
请记住,这会在将缓存移动到外部目录时保留缓存,因此请小心,因为这会在卸载应用程序时留下文件。
Fresco 还允许您提供自定义 DiskCacheConfig
and you can create a new DiskCacheConfig.Builder
and call setBaseDirectoryPath(File)
to change the path to a different folder (e.g. one on the SD card) and you can also change the directory name with setBaseDirectoryName(String)
有关 Fresco 如何进行缓存的更多信息:http://frescolib.org/docs/caching.html
我希望 fresco 在连接到 Internet 时下载图像并将其保存到 SD 卡。 稍后离线时,即使缓存被清除,我仍然需要壁画来显示保存的图像。 这可能吗?如果是,如何?
清除缓存后,简单地将图像保存到磁盘缓存似乎不起作用。
您需要在下载图片时手动将图片保存到磁盘。显示图像时,检查图像是否在磁盘中:如果不是,从 url 下载(并保存到磁盘)。
Fresco 为您缓存图像。如果您处于离线状态,图像仍应显示。你应该不需要做任何事情。
然而,当缓存被清除时(例如,当用户按下按钮或设备 space 电量低时),图像显然会从缓存中删除 - 这是不应更改的预期行为.
有 2 个选项:保存所选项目、移动缓存
保存所选项目
如果你想保留选定的图像(例如"Save"按钮),你可以获取编码图像并将其保存在设备上的某个位置。 您不应对所有图像都执行此操作,因为它们将在磁盘上出现 2 次,并且清除缓存/卸载应用程序会在设备上留下 1 个副本。
类似这样的方法可行:
DataSource<CloseableReference<PooledByteBuffer>>
dataSource = Fresco.getImagePipeline().fetchEncodedImage(imageRequest, callerContext);
dataSource.subscribe(new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
@Override
protected void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
CloseableReference<PooledByteBuffer> encodedImage = dataSource.getResult();
if (encodedImage != null) {
try {
// save the encoded image in the PooledByteBuffer
} finally {
CloseableReference.closeSafely(encodedImage);
}
}
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
// something went wrong
}
}, executorService);
}
有关如何使用管道获取编码图像的更多信息:http://frescolib.org/docs/using-image-pipeline.html
移动缓存
请记住,这会在将缓存移动到外部目录时保留缓存,因此请小心,因为这会在卸载应用程序时留下文件。
Fresco 还允许您提供自定义 DiskCacheConfig
and you can create a new DiskCacheConfig.Builder
and call setBaseDirectoryPath(File)
to change the path to a different folder (e.g. one on the SD card) and you can also change the directory name with setBaseDirectoryName(String)
有关 Fresco 如何进行缓存的更多信息:http://frescolib.org/docs/caching.html