Android的 - 而载入位图到ArrayList中使用UniversalImageLoader延迟 [英] android - delay while loading Bitmaps into ArrayList using UniversalImageLoader

查看:205
本文介绍了Android的 - 而载入位图到ArrayList中使用UniversalImageLoader延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的位图加载到ArrayList中,然后将其转换为位图[]和传入ArrayAdapter膨胀的ListView。我用UniversalImageLoader库,这里是我的code:

 最后的ArrayList<&位图GT; imgArray =新的ArrayList<>(); //方法范围之前,作为一类场
//...some code ...
文件cacheDir = StorageUtils.getOwnCacheDirectory(
            getApplicationContext(),
            / SD卡/ Android的/数据/ random_folder_for_cache);    DisplayImageOptions选项=新DisplayImageOptions.Builder()
            .cacheInMemory(真).cacheOnDisc(真).build();    ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(
            getApplicationContext())。defaultDisplayImageOptions(选项)
            //.di​​scCache(new FileCounterLimitedCache(cacheDir,100)) - 我评论它,因为FileCounterLimitedCache无法识别由于某种原因
            。建立();
    。ImageLoader.getInstance()的init(配置);    对(INT NUM = 0;民4;; NUM ++){
        ImageLoader的ImageLoader的= ImageLoader.getInstance();
        最终诠释constNum = NUM​​;
        imageLoader.loadImage(http://example.com/sample.jpg,新SimpleImageLoadingListener()
        {
            @覆盖
            公共无效onLoadingComplete(字符串imageUri,观景,位图loadedImage)
            {
                imgArray.add(constNum,loadedImage);
            }
        });
    }

不过,我有一些问题作斗争。首先,它常常返回错误(抛出IndexOutOfBoundsException - 当前指数超过ArrsyList的大小)时,第一次运行。随后一段时间(约一分钟)后的ArrayList的大小为1(我敬酒检查它),然后再次右键一次运行时,它已经4(因为它需要)。奇怪。但主要的一点,我需要的ArrayList是首先填充,然后所有的其他操作完成(它们被推迟,我没有在第一次运行错误)。怎么办呢?

和如果有人没有SD卡怎么办?顺便说一句,我找不到我的SD创建的缓存文件夹...


解决方案

通用图像装载机库包含以下方法:

 公共无效displayImage(java.lang.String中的URI,android.widget.ImageView ImageView的)

这可以通过图片网址和的的ImageView在其上显示图像。
加上,如果以后在同一的ImageView 传递给方法,但使用不同的URL两招可以发生:


  

      
  1. 如果旧形象已经下载,通常新的图像将被设置为ImageView的。


  2.   
  3. 如果旧形象还是下载库将取消正在下载的旧形象HTTP连接。并开始下载
      新的图像。 (可以在LogCat中可以观察到)。这种行为发生
      使用适配器时。


  4.   

方法调用:

<$p$p><$c$c>ImageLoader.getInstance().displayImage(\"http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/bd/Earthshaker.png\", imageView1);

配置:

如果没有指定defaultDisplayImageOptions缓存将无法正常工作。它告诉图书馆,在那里保存这些图像。因为这个图书馆有选项来加载从资产中,图形或因特网上的图像:

  DisplayImageOptions选择采用=新DisplayImageOptions.Builder()cacheInMemory(真).cacheOnDisk(真).build();


  

通过该选项,图像将被保存在应用程序。内部存储器。
  不用担心天气的设备有一个外部存储器。


  ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(本)
                .defaultDisplayImageOptions(选)
                .memoryCache(新LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .writeDebugLogs()
                。建立();。ImageLoader.getInstance()的init(配置);

我创建了它的工作GitHub的信息库。检查出来。

I need to load Bitmaps into ArrayList, then convert it to Bitmap[] and pass into ArrayAdapter to inflate ListView. I use UniversalImageLoader library and here is my code:

final ArrayList<Bitmap> imgArray = new ArrayList<>(); //before the method scope, as a class field
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
            getApplicationContext(),
            "/sdcard/Android/data/random_folder_for_cache");

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true).cacheOnDisc(true).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext()).defaultDisplayImageOptions(options)
            //.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
            .build();
    ImageLoader.getInstance().init(config);

    for (int num=0;num<4;num++) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        final int constNum = num;
        imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
        {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                imgArray.add(constNum, loadedImage);
            }
        });
    }

But I have some issues to struggle. Firstly, it often returns error (IndexOutOfBoundsException - current index exceeds size of ArrsyList) when first run. Then after some time (about a minute) the size of ArrayList is 1 (I check it with Toast), and then when run again right at once, it's already 4 (as it needs to be). Strange. But the main thing I need that the ArrayList is first filled and then all the other actions are done (that they be delayed and I don't have errors on the first run). How to do it?

And what to do if somebody doesn't have SD card? Btw, I couldn't find the created cache folder on my SD...

解决方案

Universal Image Loader library contains the following method:

public void displayImage(java.lang.String uri, android.widget.ImageView imageView)

Which can be passes the image url and the ImageView to display the image on it. plus to that, if later the same ImageView passed to the method but with a different url two thing can happen:

  1. if old image already downloaded, normally the new image will be set to the ImageView.

  2. if old image still downloading the library will cancel the http connection that is downloading the old image. and start downloading the new image. (can be observed in the LogCat). this behaviour happens when using an adapter.

Method call:

ImageLoader.getInstance().displayImage("http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/bd/Earthshaker.png", imageView1);

Configuration:

Caching will not work if the defaultDisplayImageOptions not specified. which tells the library where to save those image. because this library has options to load images from Assets, Drawables or Internet:

DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();

With this option the images will be saved in app. internal memory. don't worry weather the device have an external memory or not.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .defaultDisplayImageOptions(opts)
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .writeDebugLogs()
                .build();

ImageLoader.getInstance().init(config);

I created a working github repository for it. check it out.

这篇关于Android的 - 而载入位图到ArrayList中使用UniversalImageLoader延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆