我在monodroid中使用android universal image loader作为我的Android应用程序。
有些时候我需要在SD卡中保存一些图像。在这种情况下,我需要在流中下载图像,然后将它们保存到SD卡中。
有没有办法用这个库按流下载图像。因为在很多情况下图像会缓存在库中吗?
答案 0 :(得分:9)
UIL可以将图像缓存在SD卡上(在DisplayImageOptions中启用缓存)。您可以为缓存定义自己的文件夹(在ImageLoaderConfiguration中)。
如果您想使用UIL从SD卡显示图像,您应该传递以下URL:
file:///mnt/sdcard/MyFolder/my_image.png
即。使用file://
前缀。
<强> UPD:强> 如果您想在SD卡上保存图像:
String imageUrl = "...";
File fileForImage = new File("your_path_to_save_image");
InputStream sourceStream;
File cachedImage = ImageLoader.getInstance().getDiscCache().get(imageUrl);
if (cachedImage != null && cachedImage.exists()) { // if image was cached by UIL
sourceStream = new FileInputStream(cachedImage);
} else { // otherwise - download image
ImageDownloader downloader = new BaseImageDownloader(context);
sourceStream = downloader.getStream(imageUrl, null);
}
if (sourceStream != null) {
try {
OutputStream targetStream = new FileOutputStream(fileForImage);
try {
IoUtils.copyStream(sourceStream, targetStream, null);
} finally {
targetStream.close();
}
} finally {
sourceStream.close();
}
}