Download Image stream from android universal image loader

会有一股神秘感。 提交于 2020-01-09 07:07:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/13855166/download-image-stream-from-android-universal-image-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!