Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Universal Image Loader URI from InputStream

i want to ask about UIL which the URI input from InputStream. Because my image source from ZIP and then i must extract it to show that image. Because the image is too big, i must use the UIL library, anybody know how to insert UIL from InputStream.

like image 543
user915003 Avatar asked Feb 14 '14 01:02

user915003


1 Answers

I think you can do it similar to loading images from DB - Can Universal image loader for android work with images from sqlite db?

Lets choose own scheme so our URIs will look like "stream://...".

Then implement ImageDownloader. We should catch URIs with our scheme and return image stream.

public class StreamImageDownloader extends BaseImageDownloader {

    private static final String SCHEME_STREAM = "stream";
    private static final String STREAM_URI_PREFIX = SCHEME_STREAM + "://";

    public StreamImageDownloader(Context context) {
        super(context);
    }

    @Override
    protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
        if (imageUri.startsWith(STREAM_URI_PREFIX)) {
            return (InputStream) extra;
        } else {
            return super.getStreamFromOtherSource(imageUri, extra);
        }
    }
}

Then we set this `ImageDownloader into configuration:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    ...
    .imageDownloader(new StreamImageDownloader(context))
    .build();

ImageLoader.getInstance().init(config);

And then we can do following to display image from DB:

ImageStream is = ...; // You have image stream
// You should generate some unique string ID for this stream
// Streams for the same images should have the same string ID
String imageId = "stream://" + is.hashCode();

DisplayImageOptions options = new DisplayImageOptions.Builder()
    ...
    .extraForDownloader(is)
    .build();

imageLoader.displayImage(imageId, imageView, options);
like image 147
nostra13 Avatar answered Sep 26 '22 12:09

nostra13