0

I use android universal image loader in monodroid for my android app.

some time i need to save some images in sdcard. In this cases i need to download images in streams and then save them into sdcard.

Is there any way to download images by stream with this library. because in many cases the image is cached in the library?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

1 Answers1

9

UIL can cache image on SD card (enable caching in DisplayImageOptions). You can define your own folder for cache (in ImageLoaderConfiguration).

If you want to display image from SD card using UIL you should pass URL like: file:///mnt/sdcard/MyFolder/my_image.png

I.e. use file:// prefix.

UPD: If you want save image on SD card:

    String imageUrl = "...";
    File fileForImage = new File("your_path_to_save_image");

    InputStream sourceStream;
    File cachedImage = ImageLoader.getInstance().getDiscCache().get(imageUrl);
    if (cachedImage != null && cachedImage.exists()) { // if image was cached by UIL
        sourceStream = new FileInputStream(cachedImage);
    } else { // otherwise - download image
        ImageDownloader downloader = new BaseImageDownloader(context);
        sourceStream = downloader.getStream(imageUrl, null);
    }

    if (sourceStream != null) {
        try {
            OutputStream targetStream = new FileOutputStream(fileForImage);
            try {
                IoUtils.copyStream(sourceStream, targetStream, null);
            } finally {
                targetStream.close();
            }
        } finally {
            sourceStream.close();
        }
    }
nostra13
  • 12,377
  • 3
  • 33
  • 43
  • I downloaded an image with UIL and display it on an image gallery.now, i want if user click save images i saved this image into SD. i wanna know how can i get stream of this files from UIL to save them into SD. if it possible to send a new request to UIL and it send the stream of the image.i can send the url request agin to UIL and it send me the image from its cache and i will not downlaod the image again. –  Dec 16 '12 at 08:27
  • at real i don't want to display images. i want after displaying in gallery view save the images. –  Dec 16 '12 at 08:28
  • UIL doesn't provide such functionality, but you can use some classes. I updated my answer to show how you can do what you want. – nostra13 Dec 16 '12 at 09:00
  • How can i detect that the image is downloading? when the image is downloading cachedImage.exist() return true but the image that saved is null. –  Dec 17 '12 at 11:11
  • Only if you will track loadings yourself using listener: if ```onLoadingComplete()``` was fired then image was loaded. But ```onLoadingCancelled()``` doesn't mean that image was loaded. It still can be loading. – nostra13 Dec 17 '12 at 14:04
  • It seems that the class URLConnectionImageDownloader is not anymore on the jar. I want to know how to just download the images from the server to the sd card, but it seems there is no documentation on doing that. – Sterling Diaz Apr 18 '14 at 21:48
  • `BaseImageDownloader`replaced `URLConnectionImageDownloader`. UIL doesn't provide API to just download image on SD card yet. – nostra13 Apr 19 '14 at 13:47