齐射库中的OutOFMemory错误

时间:2014-02-24 05:46:23

标签: android android-volley

这就是我正在做的事情,首先进入Customadapter的构造函数,我已经启动了图像缓存和图像加载器

imageCache = new BitmapLruCache();
mImageLoader =  new ImageLoader(newRequestQueue(context), imageCache);

然后进入我已经完成的getView()方法......

String url = imgs[position];
image.setImageUrl(url, mImageLoader);
image.setDefaultImageResId(R.drawable.no_image);
image.setErrorImageResId(R.drawable.error_image);

其中imgs是位图url的数组,而image是NetworkImageView的对象。当我执行它时,我得到了OutOfMemory的错误。这是我的logcat o / p: -

    02-24 12:08:08.845: E/dalvikvm-heap(6288): Out of memory on a 144636048-byte allocation.
02-24 12:08:08.845: I/dalvikvm(6288): "main" prio=5 tid=1 RUNNABLE
02-24 12:08:08.845: I/dalvikvm(6288):   | group="main" sCount=0 dsCount=0 obj=0x41767b50 self=0x417571d0
02-24 12:08:08.845: I/dalvikvm(6288):   | sysTid=6288 nice=0 sched=0/0 cgrp=apps handle=1075191004
02-24 12:08:08.845: I/dalvikvm(6288):   | state=R schedstat=( 0 0 0 ) utm=116 stm=35 core=1
02-24 12:08:08.845: I/dalvikvm(6288):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:815)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.content.res.Resources.loadDrawable(Resources.java:1992)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.content.res.Resources.getDrawable(Resources.java:666)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.widget.ImageView.resolveUri(ImageView.java:616)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.widget.ImageView.setImageResource(ImageView.java:349)
02-24 12:08:08.855: I/dalvikvm(6288):   at com.android.volley.toolbox.NetworkImageView$1.onResponse(NetworkImageView.java:177)
02-24 12:08:08.855: I/dalvikvm(6288):   at com.android.volley.toolbox.NetworkImageView$1$1.run(NetworkImageView.java:168)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Handler.handleCallback(Handler.java:725)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.os.Looper.loop(Looper.java:137)
02-24 12:08:08.855: I/dalvikvm(6288):   at android.app.ActivityThread.main(ActivityThread.java:5099)
02-24 12:08:08.855: I/dalvikvm(6288):   at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:08:08.865: I/dalvikvm(6288):   at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:08:08.865: I/dalvikvm(6288):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
02-24 12:08:08.865: I/dalvikvm(6288):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
02-24 12:08:08.865: I/dalvikvm(6288):   at dalvik.system.NativeStart.main(Native Method)
02-24 12:08:08.865: D/skia(6288): --- decoder->decode returned false
02-24 12:08:08.865: D/AndroidRuntime(6288): Shutting down VM
02-24 12:08:08.865: W/dalvikvm(6288): threadid=1: thread exiting with uncaught exception (group=0x41766ae0)
02-24 12:08:08.875: E/AndroidRuntime(6288): FATAL EXCEPTION: main
02-24 12:08:08.875: E/AndroidRuntime(6288): java.lang.OutOfMemoryError
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:815)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.content.res.Resources.loadDrawable(Resources.java:1992)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.content.res.Resources.getDrawable(Resources.java:666)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.widget.ImageView.resolveUri(ImageView.java:616)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.widget.ImageView.setImageResource(ImageView.java:349)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.volley.toolbox.NetworkImageView$1.onResponse(NetworkImageView.java:177)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.volley.toolbox.NetworkImageView$1$1.run(NetworkImageView.java:168)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Handler.handleCallback(Handler.java:725)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.os.Looper.loop(Looper.java:137)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at android.app.ActivityThread.main(ActivityThread.java:5099)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
02-24 12:08:08.875: E/AndroidRuntime(6288):     at dalvik.system.NativeStart.main(Native Method)

我找不到任何对我有用的解决方案,那么我该怎么做......

2 个答案:

答案 0 :(得分:1)

感谢Itai Hanski先生,

在你提出关于OOM与网络组件没有关系的OOM之后,我已经查看了其他任何原因,最后我找到了解决方案:

来自listview项目的布局文件,我已将android:src=""设置为Imageview。 当我从ImageView中删除此xml属性时,OOM问题将解决。

但有一点我还没发现,当我为错误或加载条件设置图像时,错误再次出现,我仍然无法理解它的真正原因。

答案 1 :(得分:0)

位图是android中的一个真正问题。

首先 - 我真的建议您在开发者网站上阅读this post

您的BitmapLruCache如何实施?这里有几个指针(你只能使用它们中的一些):

  1. 限制缓存可以容纳的位图数。
  2. 限制缓存本身的大小。 Android人员提供了一个很好的算法来决定链接文章中的大小。
  3. 如果可能,请致电bitmap.recycle()
  4. 对缓存中的位图使用弱/软引用,而不是强引用。
  5. 查看此SO answer以查找类似的问题(无耻的自我插件,但相关)。
  6. 您可能不需要同时实现所有这些功能。选择适合你的那些。