5

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.

hasnain_ahmad
  • 325
  • 6
  • 17
user915003
  • 53
  • 1
  • 6

2 Answers2

6

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);
Community
  • 1
  • 1
nostra13
  • 12,377
  • 3
  • 33
  • 43
  • Hi @NOSTRA, Implementing your answer i faced this problem (http://stackoverflow.com/questions/39654279/java-inputsream-hascode-is-different-each-time-even-when-the-request-is-same). Do you have any idea about this? – Sudhir Sep 23 '16 at 06:52
1

Acceptable paths

String imageUri = "http://someurl.com/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)

Then show the image

imageLoader.displayImage(imageUri, imageView);

from documentation: https://github.com/nostra13/Android-Universal-Image-Loader

Sharjeel
  • 15,588
  • 14
  • 58
  • 89