user1682893
user1682893

Reputation:

Download Image stream from android universal image loader

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?

Upvotes: 0

Views: 7341

Answers (1)

nostra13
nostra13

Reputation: 12407

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

Upvotes: 9

Related Questions