Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i just download image with universal-image-loader

as far as I know, universal-image-loader provide 2 kinds of methods to display images. imageLoader.loadImage and imageLoader.displayImage. But those 2 methods must bind to UI element to display. Can I just download files for cache in a thread (for future display). I don't need to display those image right now.

like image 367
scott Avatar asked Apr 15 '13 13:04

scott


People also ask

What is a loader image?

An Image Loader is a JavaScript function that is responsible for taking an Image Id for an Image and returning the corresponding Image Load Object for that Image to Cornerstone. The Image Load Object contains a Promise which resolves to produce an Image.


2 Answers

You can still use UIL. Based on the displayOptions used below the images would be cached.

Refer here - https://github.com/nostra13/Android-Universal-Image-Loader

// Load image, decode it to Bitmap and return Bitmap to callback

imageLoader.loadImage(imageUri, displayOptions, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});
like image 119
Robin Srivastava Avatar answered Sep 28 '22 07:09

Robin Srivastava


Can I just download files for cache in a thread (for future display). I don't need to display those image right now.

You can download files using Executor or creating a thread. You don't need to use universal imageloader.

http://developer.android.com/reference/java/util/concurrent/Executor.html.

You can also use a DownloadManager and save the file in sdcard. You can retrieve the same for later use.

http://oodlestechnologies.com/blogs/Downloading-and-Retrieving-Files-on-SD-card-in-Android-using-Android-SDK-in-Eclipse

To cache bitmaps you can write the images to a folder in sdcard.

Caching bitmaps

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html.

You cache bitmaps in memory or disk. The link has details regarding the topic.

You basically use UIL ofr displaying images in listview or grdiview. To use UIL in listview or gridview you can do as below.

https://github.com/nostra13/Android-Universal-Image-Loader. It is based on Lazy List(works on same principle). But it has lot of other configurations. You can display a error image if downlaod failed. Can display images with rounded corners. Can cache on disc or memory. Can compress image.

In your custom adapter constructor

 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();

In your getView()

 ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
 imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options

You can configure with other options to suit your needs.

Along with lazy loading/Universal Image Loader you can view holder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.

like image 22
Raghunandan Avatar answered Sep 28 '22 08:09

Raghunandan