Search code examples
javaandroidxmlimageviewuniversal-image-loader

Universal Image Loader (UIL) makes the images too small when loaded into image views


I'm trying to use Universal Image Loader (UIL) in my test project, and I think I'm having trouble loading images when their height is greater than their width. The images loaded by UIL to my image view are too small, even if the images are actually large in size. So when I stretch my image views to match their parent, the images appear to be severely blurred and in low quality.

But UIL loads my images correctly when their height is less than or equal to their width.

Here is my java code:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.NONE)
            .bitmapConfig(Bitmap.Config.ARGB_8888)
            .build();
File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .threadPoolSize(1)
            .diskCache(new UnlimitedDiskCache(cacheDir))
            .diskCacheExtraOptions(480, 320, null)
            .build();
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(config);

String imageURL;

ImageView imageView1 = (ImageView)findViewById(R.id.imageview1);
imageURL = "http://www.up.edu.ph/wp-content/uploads/2017/01/twsc-40th-posters-WEB-01.png";
imageLoader.displayImage(imageURL, imageView1);

ImageView imageView2 = (ImageView)findViewById(R.id.imageview2);
imageURL = "http://www.up.edu.ph/wp-content/uploads/2017/01/UPD-Chancy-Selection1.png";
imageLoader.displayImage(imageURL, imageView2);

And here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ph.edu.up.e.universalimageloadersample.MainActivity"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageview1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop" />

            <ImageView
                android:id="@+id/imageview2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

This is how the images look like when loaded by UIL:

imageView1

imageView2

Please don't suggest any other image loaders. Besides that, any help would be appreciated. Thanks!


Solution

  • I've made a stupid mistake, but I've finally found a solution. Be careful when using:

    .diskCacheExtraOptions(480, 320, null)
    

    it may reduce your image quality regardless of what your image scale type is.

    IMPORTANT: When using

    .cacheOnDisk(true)
    

    delete cache/data on your emulator/phone before running your program again (just to be sure, I usually delete the whole app and reinstall it on my next run). It seems that UIL won't download better quality versions of the images later when they have been cached before, regardless of their quality.

    Hope this helps someone