user915003 user915003 - 5 months ago 25
Android Question

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.

Answer

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);
Comments