【发布时间】:2010-12-29 01:08:53
【问题描述】:
图片从网上下载后如何缓存?
【问题讨论】:
图片从网上下载后如何缓存?
【问题讨论】:
现在的重点是:使用系统缓存。
URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
Bitmap bitmap = (Bitmap)response;
}
提供内存和闪存缓存,与浏览器共享。
gr。我希望在我编写自己的缓存管理器之前有人告诉我。
【讨论】:
connection.getContent() 总是为我返回一个 InputStream,我做错了什么?
Bitmap response = BitmapFactory.decodeStream((InputStream)connection.getContent());
关于上面优雅的connection.setUseCaches 解决方案:遗憾的是,如果不付出一些额外的努力,它将无法工作。您需要使用ResponseCache.setDefault 安装ResponseCache。否则,HttpURLConnection 将默默地忽略 setUseCaches(true) 位。
详情见FileResponseCache.java顶部的cmets:
(我会在评论中发布此内容,但我显然没有足够的 SO 业力。)
【讨论】:
HttpResponseCache 时,您可能会发现 HttpResponseCache.getHitCount() 返回 0。我不确定,但我认为这是因为您请求的网络服务器不使用缓存标头案子。要使缓存正常工作,请使用connection.addRequestProperty("Cache-Control", "max-stale=" + MAX_STALE_CACHE);。
.getContent() 方法时挂起 HUC,因为 304 响应没有 RFC 标准的关联响应主体。
将它们转换为位图,然后将它们存储在 Collection(HashMap、List 等)中,或者您可以将它们写入 SD 卡。
当使用第一种方法将它们存储在应用程序空间中时,您可能希望将它们包装在 java.lang.ref.SoftReference 周围,特别是如果它们的数量很大(以便它们在运行期间被垃圾回收)危机)。不过,这可能会导致重新加载。
HashMap<String,SoftReference<Bitmap>> imageCache =
new HashMap<String,SoftReference<Bitmap>>();
将它们写入 SD 卡不需要重新加载;只是一个用户权限。
【讨论】:
Uri 路径引用,您可以将其传递给ImageView 和其他自定义视图。因为每次你compress,你都会失去质量。当然,这仅适用于有损算法。此方法还允许您存储文件的哈希值,并在下次通过 If-None-Match 和 ETag 标头从服务器请求文件时使用它。
使用LruCache 有效地缓存图像。您可以从Android Developer site 阅读有关LruCache 的信息
我使用以下解决方案在 android 中下载和缓存图像。您可以按照以下步骤操作:
第 1 步:
创建名为 ImagesCache 的类。我用过Singleton object for this class
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class ImagesCache
{
private LruCache<String, Bitmap> imagesWarehouse;
private static ImagesCache cache;
public static ImagesCache getInstance()
{
if(cache == null)
{
cache = new ImagesCache();
}
return cache;
}
public void initializeCache()
{
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() /1024);
final int cacheSize = maxMemory / 8;
System.out.println("cache size = "+cacheSize);
imagesWarehouse = new LruCache<String, Bitmap>(cacheSize)
{
protected int sizeOf(String key, Bitmap value)
{
// The cache size will be measured in kilobytes rather than number of items.
int bitmapByteCount = value.getRowBytes() * value.getHeight();
return bitmapByteCount / 1024;
}
};
}
public void addImageToWarehouse(String key, Bitmap value)
{
if(imagesWarehouse != null && imagesWarehouse.get(key) == null)
{
imagesWarehouse.put(key, value);
}
}
public Bitmap getImageFromWarehouse(String key)
{
if(key != null)
{
return imagesWarehouse.get(key);
}
else
{
return null;
}
}
public void removeImageFromWarehouse(String key)
{
imagesWarehouse.remove(key);
}
public void clearCache()
{
if(imagesWarehouse != null)
{
imagesWarehouse.evictAll();
}
}
}
第 2 步:
创建另一个名为 DownloadImageTask 的类,如果位图在缓存中不可用,它将从这里下载它:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
private int inSampleSize = 0;
private String imageUrl;
private BaseAdapter adapter;
private ImagesCache cache;
private int desiredWidth, desiredHeight;
private Bitmap image = null;
private ImageView ivImageView;
public DownloadImageTask(BaseAdapter adapter, int desiredWidth, int desiredHeight)
{
this.adapter = adapter;
this.cache = ImagesCache.getInstance();
this.desiredWidth = desiredWidth;
this.desiredHeight = desiredHeight;
}
public DownloadImageTask(ImagesCache cache, ImageView ivImageView, int desireWidth, int desireHeight)
{
this.cache = cache;
this.ivImageView = ivImageView;
this.desiredHeight = desireHeight;
this.desiredWidth = desireWidth;
}
@Override
protected Bitmap doInBackground(String... params)
{
imageUrl = params[0];
return getImage(imageUrl);
}
@Override
protected void onPostExecute(Bitmap result)
{
super.onPostExecute(result);
if(result != null)
{
cache.addImageToWarehouse(imageUrl, result);
if(ivImageView != null)
{
ivImageView.setImageBitmap(result);
}
else if(adapter != null)
{
adapter.notifyDataSetChanged();
}
}
}
private Bitmap getImage(String imageUrl)
{
if(cache.getImageFromWarehouse(imageUrl) == null)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = inSampleSize;
try
{
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
if(imageWidth > desiredWidth || imageHeight > desiredHeight)
{
System.out.println("imageWidth:"+imageWidth+", imageHeight:"+imageHeight);
inSampleSize = inSampleSize + 2;
getImage(imageUrl);
}
else
{
options.inJustDecodeBounds = false;
connection = (HttpURLConnection)url.openConnection();
stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
return image;
}
}
catch(Exception e)
{
Log.e("getImage", e.toString());
}
}
return image;
}
第 3 步:您的 Activity 或 Adapter 的使用情况
注意:如果你想从Activity类的url加载图片。使用DownloadImageTask 的第二个构造函数,但如果您想显示来自Adapter 的图像,请使用DownloadImageTask 的第一个构造函数(例如,您在ListView 中有一个图像并且您正在从“适配器”设置图像)
活动使用情况:
ImageView imv = (ImageView) findViewById(R.id.imageView);
ImagesCache cache = ImagesCache.getInstance();//Singleton instance handled in ImagesCache class.
cache.initializeCache();
String img = "your_image_url_here";
Bitmap bm = cache.getImageFromWarehouse(img);
if(bm != null)
{
imv.setImageBitmap(bm);
}
else
{
imv.setImageBitmap(null);
DownloadImageTask imgTask = new DownloadImageTask(cache, imv, 300, 300);//Since you are using it from `Activity` call second Constructor.
imgTask.execute(img);
}
适配器的用法:
ImageView imv = (ImageView) rowView.findViewById(R.id.imageView);
ImagesCache cache = ImagesCache.getInstance();
cache.initializeCache();
String img = "your_image_url_here";
Bitmap bm = cache.getImageFromWarehouse(img);
if(bm != null)
{
imv.setImageBitmap(bm);
}
else
{
imv.setImageBitmap(null);
DownloadImageTask imgTask = new DownloadImageTask(this, 300, 300);//Since you are using it from `Adapter` call first Constructor.
imgTask.execute(img);
}
注意:
cache.initializeCache() 您可以在应用程序的第一个 Activity 中使用此语句。初始化缓存后,如果您使用的是 ImagesCache 实例,则无需每次都对其进行初始化。
我从不擅长解释事情,但希望这将有助于初学者如何使用LruCache 进行缓存及其用法:)
编辑:
现在有非常著名的库被称为Picasso 和Glide,它们可以用来在android 应用程序中非常有效地加载图像。试试这个非常简单实用的库Picasso for android 和Glide For Android。您无需担心缓存图像。
Picasso 允许在您的应用程序中轻松加载图像——通常 一行代码!
Glide,就像毕加索一样,可以加载和显示来自许多 源,同时还负责缓存和保持低内存 进行图像处理时的影响。已被官方使用 Google 应用程序(如 Google I/O 2015 的应用程序)同样受欢迎 作为毕加索。在本系列中,我们将探讨差异和 Glide 优于 Picasso 的优势。
您也可以访问difference between Glide and Picasso的博客
【讨论】:
LruCache 具有键值对,每当您获得图像网址时,getImage() 都会从网址下载图像。图像的 Url 将是 LruCache 的 key 和 Bitmap 将是值,如果您仔细查看 DownloadImageTask 您可以设置 desiredWidth 和 desiredHeight 值您设置较小的宽度和高度您将看到的图像质量越低。
if(cache == null) 解决了我的问题! :)
要下载图像并保存到存储卡,您可以这样做。
//First create a new URL object
URL url = new URL("http://www.google.co.uk/logos/holiday09_2.gif")
//Next create a file, the example below will save to the SDCARD using JPEG format
File file = new File("/sdcard/example.jpg");
//Next create a Bitmap object and download the image to bitmap
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
//Finally compress the bitmap, saving to the file previously created
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
不要忘记在清单中添加 Internet 权限:
<uses-permission android:name="android.permission.INTERNET" />
【讨论】:
我会考虑使用 droidfu 的图像缓存。它实现了内存和基于磁盘的图像缓存。您还将获得一个利用 ImageCache 库的 WebImageView。
这里是 droidfu 和 WebImageView 的完整描述: http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/
【讨论】:
我已经尝试过 SoftReferences,它们在 android 中被过度回收,我觉得使用它们没有意义
【讨论】:
SoftReferences。他们建议改用LruCache。
正如 Thunder Rabbit 所建议的,ImageDownloader 是最适合这项工作的。我还在以下位置发现了类的细微变化:
http://theandroidcoder.com/utilities/android-image-download-and-caching/
两者的主要区别在于ImageDownloader使用Android缓存系统,而修改后的使用内部和外部存储作为缓存,将缓存的图像无限期保留或直到用户手动删除。作者还提到了 Android 2.1 的兼容性。
【讨论】:
这是乔的一个很好的收获。上面的代码示例有两个问题 - 一个 - 响应对象不是 Bitmap 的实例(当我的 URL 引用 jpg 时,例如 http:\website.com\image.jpg,它是一个
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$LimitedInputStream)。
其次,正如 Joe 所指出的,如果没有配置响应缓存,就不会发生缓存。 Android 开发人员只能滚动自己的缓存。这是一个这样做的示例,但它只缓存在内存中,这确实不是完整的解决方案。
http://codebycoffee.com/2010/06/29/using-responsecache-in-an-android-app/
此处描述了 URLConnection 缓存 API:
http://download.oracle.com/javase/6/docs/technotes/guides/net/http-cache.html
我仍然认为这是走这条路的好解决方案 - 但您仍然必须编写缓存。听起来很有趣,但我宁愿写功能。
【讨论】:
在Android官方培训版中有专门的介绍:http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
该部分很新,提出问题时它不存在。
建议的解决方案是使用 LruCache。该类是在 Honeycomb 上引入的,但它也包含在兼容性库中。
您可以通过设置最大数量或条目来初始化 LruCache,当您超过限制时,它会自动对它们进行排序并清理它们。除此之外,它还用作普通地图。
来自官方页面的示例代码:
private LruCache mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
以前 SoftReferences 是一个很好的选择,但现在不再是,引用官方页面:
注意:过去,流行的内存缓存实现是 SoftReference 或 WeakReference 位图缓存,但这不是 推荐的。从 Android 2.3 (API Level 9) 开始的垃圾 收集器在收集软/弱引用方面更具侵略性 这使得它们相当无效。此外,在 Android 3.0 之前 (API 级别 11),位图的支持数据存储在本地 未以可预测的方式释放的内存,可能 导致应用程序短暂超出其内存限制并崩溃。
【讨论】:
考虑使用Universal Image Loader library by Sergey Tarasevich。它附带:
通用图像加载器允许对下载的图像进行详细的缓存管理,具有以下缓存配置:
UsingFreqLimitedMemoryCache:当超过缓存大小限制时,最不频繁使用的位图被删除。LRULimitedMemoryCache:当超过缓存大小限制时,最近最少使用的位图将被删除。FIFOLimitedMemoryCache:超过缓存大小限制时使用FIFO规则进行删除。LargestLimitedMemoryCache:当超过缓存大小限制时,最大的位图被删除。LimitedAgeMemoryCache: Cached 对象在其年龄超过定义值时被删除。WeakMemoryCache: 只对位图有弱引用的内存缓存。一个简单的用法示例:
ImageView imageView = groupView.findViewById(R.id.imageView);
String imageUrl = "http://site.com/image.png";
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
imageLoader.displayImage(imageUrl, imageView);
本示例使用默认的UsingFreqLimitedMemoryCache。
【讨论】:
实际上对我有用的是在我的 Main 类上设置 ResponseCache:
try {
File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (IOException e) { }
和
connection.setUseCaches(true);
下载位图时。
http://practicaldroid.blogspot.com/2013/01/utilizing-http-response-cache.html
【讨论】:
Google 的 libs-for-android 有一个很好的库来管理图像和文件缓存。
【讨论】:
我已经为此苦苦挣扎了一段时间;使用 SoftReferences 的答案会很快丢失数据。建议实例化 RequestCache 的答案太混乱了,而且我永远找不到完整的例子。
但是ImageDownloader.java 对我来说非常有用。它使用 HashMap 直到达到容量或直到发生清除超时,然后将事情转移到 SoftReference,从而使用两全其美。
【讨论】:
我建议IGNITION这比Droid fu还要好
https://github.com/kaeppler/ignition
https://github.com/kaeppler/ignition/wiki/Sample-applications
【讨论】:
甚至稍后回答,但我写了一个 Android 图像管理器,它可以透明地处理缓存(内存和磁盘)。代码在 Github https://github.com/felipecsl/Android-ImageManager
【讨论】:
迟到的答案,但我想我应该添加一个指向我的网站的链接,因为我已经写了一个教程如何为 android 制作图像缓存:http://squarewolf.nl/2010/11/android-image-cache/ 更新: 由于来源已过时,该页面已脱机。我和@elenasys 一起建议使用Ignition。
致所有偶然发现这个问题但尚未找到解决方案的人:希望你们喜欢! =D
【讨论】:
迟到的答案,但我认为这个库对缓存图像有很大帮助: https://github.com/crypticminds/ColdStorage.
简单地用注解 ImageView @LoadCache(R.id.id_of_my_image_view, "URL_to_downlaod_image_from) 它将负责下载图像并将其加载到图像视图中。您还可以指定占位符图像和加载动画。
注释的详细文档在这里:- https://github.com/crypticminds/ColdStorage/wiki/@LoadImage-annotation
【讨论】: