2014-02-14 83 views

回答

6

我認爲你可以做到這一點類似於從數據庫加載圖像 - Can Universal image loader for android work with images from sqlite db?

讓我們選擇自己的方案吧r 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); 
+0

@Audrius KI不知道爲什麼版主拒絕你的編輯:) – NOSTRA

+0

嗨@NOSTRA,實現你的答案我面臨這個問題(http://stackoverflow.com/questions/39654279/java-inputsream-hascode-is-different-每個時間偶數時最請求是-相同)。你有什麼想法嗎? – Sudhir

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

相關問題