加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 图像未被缓存在本地(使用通用图像加载程序) – 缓慢的图

发布时间:2020-12-14 17:47:07 所属栏目:Java 来源:网络整理
导读:问题描述: 我正在创建一个可滚动列表的文章,缩略图由我的SQLite数据库填充.一般来说,它是“工作”,除了缓慢: 图像加载非常缓慢…我以为使用“Universal Image Loader”缓存设备上的图像,这将使它们似乎只是滚动到视图,如果你已经查看了(或至少接近).但是
问题描述:
我正在创建一个可滚动列表的文章,缩略图由我的SQLite数据库填充.一般来说,它是“工作”,除了缓慢:

图像加载非常缓慢…我以为使用“Universal Image Loader”缓存设备上的图像,这将使它们似乎只是滚动到视图,如果你已经查看了(或至少接近).但是 – 当您向上/向下拖动时,图像中没有任何图像,然后3-5秒钟后,图像开始弹出(就像重新下载它们)

我正在更改缩略图框的可见性,但是这是完美无缺的 – 它们似乎没有改变 – 它们只是滚动到视图中,不闪烁或任何东西. (但是图像不会再出现几秒钟).

我已经通过删除我的php脚本,滚动后…当我回滚到上一个点,图像不显示 – 让我假设它从我的PHP脚本加载每一次.

但是根据the docs:“UsingFreqLimitedMemoryCache(超过缓存大小限制时,最常使用的位图被删除) – 默认使用”

细节:

在我的ArticleEntryAdapter.js中我有:

@Override
public View getView(final int position,final View convertView,final ViewGroup parent) {

    // We need to get the best view (re-used if possible) and then
    // retrieve its corresponding ViewHolder,which optimizes lookup efficiency
    final View view = getWorkingView(convertView);
    final ViewHolder viewHolder = getViewHolder(view);
    final Article article = getItem(position);

    // Set the title
    viewHolder.titleView.setText(article.title);

    //Set the subtitle (subhead) or description
    if(article.subtitle != null)
    {
        viewHolder.subTitleView.setText(article.subtitle);
    }
    else if(article.description != null)
    {
        viewHolder.subTitleView.setText(article.description);
    }

    ImageLoader imageLoader = ImageLoader.getInstance();

    imageLoader.displayImage("",viewHolder.thumbView); //clears previous one
    if(article.filepath != null && article.filepath.length() != 0) {
        imageLoader.displayImage(
            "http://img.sltdb.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",viewHolder.thumbView
            );
        viewHolder.thumbView.setVisibility(View.VISIBLE);
    } else {
        viewHolder.thumbView.setVisibility(View.GONE);
    }

    return view;
}

只要图像不正确 – 它不是经常的,但有时滚动时,我会看到相同的图像2,当我看文章时,它们并不完全相关(即没有机会实际使用相同的图像)所以 – 我滚开它,并回来,它不再是不正确的图像.

注意:我是Java / Android的新手 – 您可能已经注意到了.

每个注释请求的更多代码:

private View getWorkingView(final View convertView) {
    // The workingView is basically just the convertView re-used if possible
    // or inflated new if not possible
    View workingView = null;

    if(null == convertView) {
        final Context context = getContext();
        final LayoutInflater inflater = (LayoutInflater)context.getSystemService
          (Context.LAYOUT_INFLATER_SERVICE);

        workingView = inflater.inflate(articleItemLayoutResource,null);
    } else {
        workingView = convertView;
    }

    return workingView;
}

更新:
我的清单文件有:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但是我发现的缓存文件夹完全是空的:

mnt
  -sdcard
    -Android
      -data
        -com.mysite.news
          -cache
            -uil-images

解决方法

我在列表视图中遇到与图像类似的问题.可能 this answer将纠正您的错误图像问题.

我刚刚使用UniversalImageLoader下载了示例项目,并展示了您所描述的相同行为.

到目前为止,几个注释看不清源代码.

public static final int DEFAULT_THREAD_POOL_SIZE = 3;
public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
public static final int DEFAULT_MEMORY_CACHE_SIZE = 2 * 1024 * 1024; // bytes

这表示在任何时候都会有三个线程下载,最多2MB的图像.你下载的图片有多大?你还缓存到磁盘?如果是这样,那将会很慢.

要配置ImageLoader中的一些基本选项,您需要传入displayImage:

DisplayImageOptions options = new DisplayImageOptions.Builder()
     .showStubImage(R.drawable.stub_image)
     .cacheInMemory()
     .cacheOnDisc()
     .build();

我也想让你尝试这些选项:

ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
    .enableLogging()
    .memoryCacheSize(41943040)
    .discCacheSize(104857600)
    .threadPoolSize(10)
    .build();

imageLoader = ImageLoader.getInstance();
imageLoader.init(imageLoaderConfiguration);

通过我的测试,图像在磁盘上,但加载仍然很慢.

经过广泛的测试,我确定的主要问题是,UniversalImageLoader只是缓慢.具体来说,ImageLoader和LoadAndDisplayImageTask正在保留作品.我(很快)重写了作为AsyncTask的LoadAndDisplayImageTask,它立即执行得更好.您可以在GitHub上下载代码的分叉版本.

Universal Image Loader with AsyncTasks

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读