Search code examples
androidimagegridviewuniversal-image-loader

Android Universal Image Loader slow, ignores ImageView height and width


I am using the Universal Image Loader library for a multiple image picker to display photos from phone's default gallery in a GridView, it displays all my images but really slow.

I have tried to follow their docs, and set everything to optimise for a quicker image display. At one point it suggests to set a fixed images size for my ImageView, I am doing so (110dp x 100dp) but it still loads slow and in the log I see that my images have a size of 220 x 240 dp. Why does it ignore my size settings?

To be clear, I would like to optimaize the UIL library with a fixed image size as it shows in the documentation, not to fit the images into my imageview.

image item inside gridview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:padding="1dp" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@+id/frmQueue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imgQueue"
                android:layout_width="110dp"
                android:layout_height="120dp"
                android:contentDescription="@string/app_name"

                android:src="@drawable/no_media" />
        </FrameLayout>

        <ImageView
            android:id="@+id/imgQueueMultiSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="2dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/on_focus_checkbox"
            android:contentDescription="@string/app_name"
            android:gravity="center"
            android:scaleType="center"
            android:visibility="gone" />
    </FrameLayout>
</LinearLayout>

settings of the library instance:

private void initImageLoader() {

    try {

        String CACHE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.temp_tmp";
        new File(CACHE_DIR).mkdirs();

        File cacheDir = StorageUtils.getOwnCacheDirectory(getBaseContext(), CACHE_DIR);

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheOnDisc(true).imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565).build();//.cacheInMemory(true)

        ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(getBaseContext()).threadPoolSize(3).defaultDisplayImageOptions(defaultOptions).discCache(new UnlimitedDiscCache(cacheDir)).memoryCache(new WeakMemoryCache());                  

        ImageLoaderConfiguration config = builder.build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
        //L.disableLogging();
    } catch (Exception e) {}
}

Solution

  •  private void initImageLoader() {
        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.default_img)
                .showImageOnLoading(R.drawable.default_img)
                .cacheOnDisk(true).build();
    
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCacheSize(20 * 1024 * 1024) // 20 Mb
                .denyCacheImageMultipleSizesInMemory()
                .diskCache(new UnlimitedDiscCache(getCacheDir()))
                .diskCacheSize(20 * 1024 * 1024) //20 Mb
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                        //.enableLogging() // Not necessary in common
                .defaultDisplayImageOptions(displayImageOptions)
                .threadPoolSize(30)
                .build();
    
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
    }
    

    This is my configuration, Please try to replicate it once. This is working fine for me.