android-在ListView中使用ArrayAdapter的OutOfMemoryError
作者:互联网
我使用一个以这种方式实现getView()方法的ArrayAdapter创建了一个ListView:
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView ==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.rowlayout_member, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.label);
TextView textView1 = (TextView) rowView.findViewById(R.id.label1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
textView1.setText(surname[position]);
String s = values[position];
if(foto[position] != null){
Drawable c = Drawable.createFromPath(basePath+foto[position]); // line 56
if(c!=null)
imageView.setImageDrawable(c);
else
imageView.setImageResource(R.drawable.default);
}else{
imageView.setImageResource(R.drawable.default);
}
return rowView;
}
一切似乎都正常,但是当我多次进入列表并进入列表(更改活动)时,有时会出现以下错误:
08-06 15:06:14.110: E/dalvikvm-heap(3527): Out of memory on a 2380816-byte allocation.
08-06 15:06:14.110: W/dalvikvm(3527): threadid=1: thread exiting with uncaught exception (group=0x40a3d1f8)
08-06 15:06:14.120: E/AndroidRuntime(3527): FATAL EXCEPTION: main
08-06 15:06:14.120: E/AndroidRuntime(3527): java.lang.OutOfMemoryError
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:880)
08-06 15:06:14.120: E/AndroidRuntime(3527): at host.framework.component.MySimpleArrayAdapter.getView(MySimpleArrayAdapter.java:56)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView.obtainView(AbsListView.java:2012)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.makeAndAddView(ListView.java:1772)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.fillUp(ListView.java:705)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.ListView.fillGap(ListView.java:645)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3813)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Handler.handleCallback(Handler.java:605)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.os.Looper.loop(Looper.java:137)
08-06 15:06:14.120: E/AndroidRuntime(3527): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-06 15:06:14.120: E/AndroidRuntime(3527): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 15:06:14.120: E/AndroidRuntime(3527): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 15:06:14.120: E/AndroidRuntime(3527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 15:06:14.120: E/AndroidRuntime(3527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 15:06:14.120: E/AndroidRuntime(3527): at dalvik.system.NativeStart.main(Native Method)
08-06 15:06:14.120: W/ActivityManager(149): Force finishing activity host.activity/.ACT_CircoliDiSupporto
08-06 15:06:14.640: W/ActivityManager(149): Activity pause timeout for ActivityRecord{413de508 host.activity/.ACT_CircoliDiSupporto}
08-06 15:06:24.140: W/ActivityManager(149): Launch timeout has expired, giving up wake lock!
08-06 15:06:24.650: W/ActivityManager(149): Activity idle timeout for ActivityRecord{41389d58 host.activity/.HostActivity}
第56行(代码中突出显示)似乎会产生错误
Drawable c = Drawable.createFromPath(basePath+foto[position]);
解决方法:
当前的实现存在许多问题:
>代码访问并在主/ UI线程上构建位图(Drawable c = Drawable.createFromPath(basePath foto [position]); //第56行),这会使列表滚动得很不连贯
>每次请求视图时都会创建一个新的drawable,在2.x Android系统上,Garbage Collector(GC)会收集一段时间未引用的位图,从而加剧了内存问题
>您从存储中加载图像时未将其缩放到适当的大小,这可能会或可能不会造成问题,因为我不知道您正在显示的图像的尺寸还是图像的尺寸
我建议您使用Universal Image Loader库(https://github.com/nostra13/Android-Universal-Image-Loader),它可以解决所有上述问题.尽管我不确定它是否提供本机本地图像加载功能,或者您是否必须扩展某些类才能具有这种功能.
[编辑]
我不建议您使用自己的解决方案,因为它比您最初想象的要花费更多的时间(相信我…).但是,如果您真的想这样做,可以查看本教程:http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/
上面的教程解决了#1& #2,对于#3,请检查:Strange out of memory issue while loading an image to a Bitmap object
标签:listview,android-listview,android-arrayadapter,android 来源: https://codeday.me/bug/20191030/1968915.html