Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android后台线程中从磁盘加载缓存图像_Android_Multithreading_Caching - Fatal编程技术网

android后台线程中从磁盘加载缓存图像

android后台线程中从磁盘加载缓存图像,android,multithreading,caching,Android,Multithreading,Caching,我有一个列表视图,其中图像从internet加载,然后缓存在磁盘上。在滚动时,我试图使用ExecutorService从后台线程中的磁盘加载图像(因为滚动时会有多个图像)-如下所示: executorService.submit(new Runnable() { @Override public void run() { // load images from the disk // reconnect with UI thread u

我有一个列表视图,其中图像从internet加载,然后缓存在磁盘上。在滚动时,我试图使用ExecutorService从后台线程中的磁盘加载图像(因为滚动时会有多个图像)-如下所示:

executorService.submit(new Runnable() {
    @Override
    public void run() {
           // load images from the disk
           // reconnect with UI thread using handler
        }
}
然而,滚动一点也不平滑,而且非常急促——就好像UI线程在某处被阻塞一样。但是当我对给定的代码进行注释时,滚动是平滑的。我无法理解我的实现中的缺陷


编辑:刚才我发现问题发生在我从后台线程将消息传递到UI线程时。如果我对该部分进行注释,则滚动是平滑的(但图像当然不会显示)

您可以使用延迟加载或通用图像加载程序

延迟列表是使用URL从SD卡或fomr服务器延迟加载图像。这就像按需加载图像

图像可以缓存到本地sd卡或手机存储器中。Url被认为是关键。如果钥匙存在于sd卡的sd卡显示图像中,则通过从服务器下载显示图像,并将其缓存到您选择的位置。可以设置缓存限制。您还可以选择自己的位置来缓存图像。缓存也可以被清除

用户不必等待下载大型图像,然后显示延迟列表,而是按需加载图像。由于图像区域已缓存,因此可以脱机显示图像

。懒惰列表

在您的getview中

imageLoader.DisplayImage(imageurl, imageview); ImageLoader Display method

public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);   //get image from cache using url as key
if(bitmap!=null)         //if image exists
imageView.setImageBitmap(bitmap);  //dispaly iamge
else   //downlaod image and dispaly. add to cache.
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
惰性列表的另一种选择是通用图像加载器

它基于惰性列表(工作原理相同)。但它有很多其他配置。我更喜欢使用通用图像加载器,因为它为您提供了更多的配置选项。如果downlaod失败,您可以显示错误图像。可以显示圆角的图像。可以缓存在磁盘或内存中。可以压缩图像

在自定义适配器构造函数中

 File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
  // You can pass your own memory cache implementation
 .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
 .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
 .enableLogging()
 .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();
在getView()中

您可以配置其他选项以满足您的需要

使用延迟加载/通用图像加载程序,您可以查看保持架,以实现平滑滚动和性能


.

您可以使用延迟加载或通用图像加载程序

延迟列表是使用URL从SD卡或fomr服务器延迟加载图像。这就像按需加载图像

图像可以缓存到本地sd卡或手机存储器中。Url被认为是关键。如果钥匙存在于sd卡的sd卡显示图像中,则通过从服务器下载显示图像,并将其缓存到您选择的位置。可以设置缓存限制。您还可以选择自己的位置来缓存图像。缓存也可以被清除

用户不必等待下载大型图像,然后显示延迟列表,而是按需加载图像。由于图像区域已缓存,因此可以脱机显示图像

。懒惰列表

在您的getview中

imageLoader.DisplayImage(imageurl, imageview); ImageLoader Display method

public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);   //get image from cache using url as key
if(bitmap!=null)         //if image exists
imageView.setImageBitmap(bitmap);  //dispaly iamge
else   //downlaod image and dispaly. add to cache.
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
惰性列表的另一种选择是通用图像加载器

它基于惰性列表(工作原理相同)。但它有很多其他配置。我更喜欢使用通用图像加载器,因为它为您提供了更多的配置选项。如果downlaod失败,您可以显示错误图像。可以显示圆角的图像。可以缓存在磁盘或内存中。可以压缩图像

在自定义适配器构造函数中

 File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
  // You can pass your own memory cache implementation
 .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
 .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
 .enableLogging()
 .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();
在getView()中

您可以配置其他选项以满足您的需要

使用延迟加载/通用图像加载程序,您可以查看保持架,以实现平滑滚动和性能


.

您可以尝试一下LazyList=>谢谢Paresh,但我已经完成了我的实现,只有提到的这个问题。如果我能知道give代码中的错误,那将非常有帮助。你做得很好。@SaurabhVerma你在使用视图保持器显示图像吗?。是的,我正在使用一个ViewHolder和一个最大大小为20的Hashmap磁盘作为缓存。您可以尝试一下LazyList=>谢谢Paresh,但是我已经完成了我的实现,只有上面提到的这个问题。如果我能知道give代码中的错误,那将非常有帮助。你做得很好。@SaurabhVerma你在使用视图保持器显示图像吗?。是的,我正在使用一个ViewHolder和一个最大大小为20的Hashmap磁盘作为缓存