来自InputStream的Android Universal Image Loader URI

时间:2014-02-14 01:18:45

标签: android universal-image-loader

我想问一下UIL从InputStream输入的URI。因为我的图像来自ZIP,然后我必须提取它来显示该图像。因为图像太大,我必须使用UIL库,任何人都知道如何从InputStream插入UIL。

2 个答案:

答案 0 :(得分:6)

我认为你可以这样做来加载来自DB的图像 - Can Universal image loader for android work with images from sqlite db?

让我们选择自己的方案,以便我们的URI看起来像“stream:// ...”

然后实施ImageDownloader。我们应该使用我们的方案捕获URI并返回图像流。

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

然后我们将`ImageDownloader设置为配置:

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

ImageLoader.getInstance()的init(配置);

然后我们可以执行以下操作来显示来自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);

答案 1 :(得分:1)

可接受的路径

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)

然后显示图像

imageLoader.displayImage(imageUri, imageView);

来自文档:https://github.com/nostra13/Android-Universal-Image-Loader