- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
URL url = new URL("http://s2.goodfon.ru/image/260463-1920x1200.jpg");
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) url.getContent(), null, options);
if(bitmap != null)
Log.i("Success", "BITMAP IS NOT NULL");
String key = "myKey";
Log.i("Get is null", "putting myKey");
mMemoryCache.put(key, bitmap);
Bitmap newBitmap = mMemoryCache.get(key);
if(newBitmap == null)
Log.i("newBitmap", "is null");
你好,这是一个代码。我成功地从 URL 获取位图(日志显示位图不为空,我可以轻松显示它)。然后我试图将它放入 LruCache 并取回它,但它返回 null。 (日志显示 newBitmap 为空)。我的错误在哪里?请告诉我。Android 4.1.2 缓存大小 8192 Kb。
最佳答案
如果它在磁盘上是 1.19 MB 但在内存中大约是 9 MB,这意味着作为一个压缩的 JPEG 文件,它是 1.19 MB,一旦你将它提取到一个可以显示的位图(未压缩)中,它将占用 9 MB 在内存中。如果它是代码 fragment 中 url 所建议的 1920 x 1200 像素图像,则该图像将占用 1920 x 1200 x 4 字节的内存(每个像素 4 个字节表示从 0 到 256 的 ARGB 值乘以 230 万总像素= 9,216,000 字节)。如果您将可用内存的 1/8 用于此缓存,则 9MB 可能/可能超过该总内存空间,因此位图永远不会进入缓存或立即被逐出。
如果图像太大(使用 BitmapFactory.Options.inSampleSize
),您可能想要在解码时对图像进行下采样...如果您使用它,网络上有很多文档我们还不熟悉)。
此外,您还使用 Runtime.maxMemory 来计算缓存大小。这意味着您正在请求允许整个 VM 使用的最大内存量。
http://developer.android.com/reference/java/lang/Runtime.html#maxMemory%28%29
更常见的方法是使用 ActivityManager.getMemoryClass() 方法返回给您的值。
这里有一个示例代码 fragment 和文档中的方法定义以供引用。
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
int memClassBytes = am.getMemoryClass() * 1024 * 1024;
int cacheSize = memClassBytes / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
http://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass%28%29
关于android - LruCache 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15459834/
我的应用程序中的多个 Activity 在 ListView 中显示图像,其中 ListView 的每一行都包含一个 ImageView. 这方面的一个示例是用户搜索、获取结果并显示每个结果的图片的搜
一 点睛 强引用(Strong Reference)是我们使用最多的一种对象引用,当一个对象被关键字 new 实例化出来的时候, JVM 会在堆(heap)内存中开辟一个内存区域,用于存放与该实例对应
我使用了代码 android lrucache example (Memory Cache)缓存下载的图像(总共将近 120 KB)并且它可以工作,但是当我从 Activity (或应用程序)中退出时
我正在阅读这篇文章来了解 Java 中的 LRUCache - Java LRU cache using LinkedList 这篇文章中有一个解决方案: public class LRUCache
我正在考虑为 Android 实现离线模式,但为了实现这一目标,我需要实现一个缓存系统。我读到人们建议 LruCache 并在这里看到了图像的示例 LruCache Example 。现在我不缓存图像
我正在尝试遵循有关 LruCache 使用的 2 年历史的 android 教程,到目前为止我在 Google 上搜索的一些示例具有相同的方法,即传递一个转换为 KiB 的值(int)。 final
我不能完全理解 LruCache 的内存管理组件。 从某种意义上说,我希望它能充当 iOS 平台上的 NSCache(最近刚刚从该操作系统中学到了一些知识)。 如果应用程序内存不足,LruCache
以下示例表明,在 LRUCache 之外分配新空间时,使用有限大小的 LRUCache 会导致 OutOfMemory 错误。 属性:64MB 进程大小; 10MB LRUCache 大小;我循环放入
我正在使用 LruCache 来缓存我的应用程序中使用的大量小 BitmapDrawables。问题是尺寸因我使用图像的不同位置而异。 我在从缓存中检索可绘制对象时设置边界,然后再将其设置为 Imag
我是 android 中 LruCache 的新手,我想在此缓存上放置和获取位图图像 (JPEG) 以防止内存错误和内存异常,所以我不明白为什么我的代码不起作用。这是我的代码: ImageView i
我已经在 Android 中实现了一个存储对象的标准 LRUCache。每个键都是与存储的对象关联的唯一 ObjectId。我的问题是从缓存中检索对象的唯一方法是通过 ObjectId(无迭代器)。实
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = max
例如对 网络加载图片进行缓存 : ? 1
我尝试解决 LeetCode 中的问题,要求实现一个LRUCache。当我提交代码时,系统告诉我结果是错误答案。 由于测试用例太长,我在代码中找不到问题。当我选择“运行代码”来提交我的代码时,它是正确
我可以在 Cachetools 的 LRUCache 实现中使用多个/组合键吗?我想像下面这样使用它 def fun(a,b): pass x = LRUCache(maxsize=100,missi
我有一个 LRUCache,用于缓存缩略图和其他类似的小位图。目前,我在我的主要 Activity 中使用它,但是,我也想在我的其他 Activity 中使用它。这让我想知道将这个 LRUCache
我从 developer.android.com 上读到了关于使用 LruCache 的信息,我从一个 Activity 中创建了一个模糊的 Bitmap 并将其放入缓存中,现在我想访问来自另一个 A
我一直在尝试在我的应用程序中实现 LruCache,但我在连接点和在不同组件之间传递位图时遇到困难。 我想知道如何在我的应用程序中集成 LruCache。我也想了解实现 LruCache 的过程,所以
我正在尝试在 android 中使用 LruCache 来缓存一些图像,但它没有缓存 这是代码 int cacheSize1 = 4 * 1024 * 1024; // 4MiB bit
我有以下代码来缓存位图,当我尝试使用它们的键检索它们时。他们总是返回 null 。请帮我 。谢谢你 final int memClass = ((ActivityManager) mcontext.
我是一名优秀的程序员,十分优秀!