Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

xamarin.android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...