我正在测试升级以使用 Android Universal Image Loader 1.9.0。我已经在 Android 4.2 上测试过了。我遇到了一个问题,我在 Intent 服务中下载和缓存图像以在后台线程上运行以不干扰用户。但是,UIL 提示我没有在主线程上加载这些图像。我不想在主线程上加载这些图像,我只是希望能够在后台将它们缓存在磁盘上,以便用户实际查看指定图像时不需要下载它们。除了 ImageLoader.loadImage 之外,还有其他方法可以在后台线程上预缓存图像吗?如果不是,我如何允许在非主线程的线程上在光盘上进行预缓存?所有这些代码都在 IntentService 中运行。
图像加载器配置:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.threadPriority(Thread.MAX_PRIORITY)
.memoryCacheSize(memoryCacheSize)
.denyCacheImageMultipleSizesInMemory()
.threadPoolSize(5)
.discCacheFileNameGenerator(new MyFileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
显示图像选项:
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc()
.imageScaleType(ImageScaleType.NONE).build();
加载图片代码:
imageLoader.loadImage(imageURI, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage)
{
loadNext(); // gives a notification update and loads next image URI in queue
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason)
{
loadNext(); // gives a notification update and loads next image URI in queue
}
});
最佳答案
实际上 UIL 不提供预加载图像的方法,即只是添加到磁盘缓存。它总是返回解码后的位图。
但在您的情况下,您可以使用 loadImageSync(...)
。我认为它更适合你。
关于android - 如何使用通用图像加载器在 Intent 服务(后台线程)中缓存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20504477/