How can i just download image with universal-image-loader
Asked Answered
D

5

11

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.

Dagnah answered 15/4, 2013 at 13:37 Comment(0)
C
5

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.

Convergence answered 15/4, 2013 at 15:30 Comment(3)
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) this is what I want, thx.Dagnah
Here when i pass directory name in new UnlimitedDiscCache(cacheDir) than this directory is not created in sdcard Why its happend?Geomorphic
have you added the write permission in manifest file. check if(cacheDir.exists()){log some message}Convergence
P
12

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
    }
});
Pood answered 23/12, 2013 at 18:15 Comment(1)
What do you mean by "return Bitmap to callback" ? I modify the loadedImage bitmap but I don't know how to display it (the modified version). Could you please elaborate ?Zuzana
C
5

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.

Convergence answered 15/4, 2013 at 15:30 Comment(3)
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) this is what I want, thx.Dagnah
Here when i pass directory name in new UnlimitedDiscCache(cacheDir) than this directory is not created in sdcard Why its happend?Geomorphic
have you added the write permission in manifest file. check if(cacheDir.exists()){log some message}Convergence
M
2

Theres loadImage(String uri, ImageLoadingListener listener), I think you can call it with null for the listener if you don't need one.

Morganmorgana answered 15/4, 2013 at 13:42 Comment(1)
java.lang.IllegalArgumentException: Wrong arguments were passed to displayImage() method (ImageView reference must not be null). I just checked by passing null in place of imageview. passing null as a parameter won't help.Convergence
M
0

Adding to @Robin Srivastava's answer:

You must also instantiate the ImageLoader context, for example: imageLoader = ImageLoader.getInstance(); before you can use the loadImage method. The displayOptions parameter is also optional so you can exclude that if need be.

Marylyn answered 28/4, 2014 at 9:50 Comment(0)
M
0

Using UIL,we can save the image when the image is fully loaded.

using ImageLoading Listener when loading is completed the listener has a method called onLoadingComplete() we can get Bitmap of the image and we can store this Bitmap using the below method saveImage()

Bitmap imageBitmap=null;
ImageLoader.getInstance().displayImage(String.valueOf(mediaPath), imageView, options, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    progressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                        case IO_ERROR:
                            message = "I/O error";
                            break;
                        case NETWORK_DENIED:
                            message = "Network Denied";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out of memory";
                            break;
                        case DECODING_ERROR:
                            message = "decoding error";
                            break;
                    }
                    Toast.makeText(FullScreenActivity.this, message, Toast.LENGTH_SHORT).show();
                    progressBar.setVisibility(View.GONE);

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    //we can get imageBitmap here when loading is completed
                    imageBitmap=loadedImage;
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });

Use this method to save the file in your local storage


 public void saveImage(){
        if(imageBitmap!=null){
            File dir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + “/Images/");
            if (!dir.exists()) {
                if (dir.mkdirs()) {
                    Log.i(TAG, "Directory created");
                }
            }
            //put your image file name here
            File mypath=new File(dir,"yourImageName.png");
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(mypath);
                // Use the compress method on the BitMap object to write image to the OutputStream
                if(imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)){
                    showToast("Successfully downloaded");
                }
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


Toast toast=null;
private void showToast(String message) {
        if (toast != null) toast.cancel();
        toast = Toast.makeText(FullScreenActivity.this, message, Toast.LENGTH_SHORT);
        toast.show();
}
Mythos answered 22/5, 2020 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.