我的应用程序在从Listview创建BitMap时抛出OutOfMemoryException。此错误是新的,但未对方法进行任何更改。
如果有人对这个问题有所了解,那就太好了。这是堆栈跟踪:
java.lang.OutOfMemoryError:
at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
at android.graphics.Bitmap.nativeCreate (Native Method)
at android.graphics.Bitmap.createBitmap (Bitmap.java:977)
at android.graphics.Bitmap.createBitmap (Bitmap.java:948)
at android.graphics.Bitmap.createBitmap (Bitmap.java:915)
at com.beerapp.givemebeerfree.utils.Helper.getWholeListViewItemsToBitmap (Helper.java:300)
at com.beerapp.givemebeerfree.fragments.ListViewFragment$4.onClick (ListViewFragment.java:196)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage (AlertController.java:162)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6776)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)
以下是方法:
public static Bitmap getWholeListViewItemsToBitmap(SearchableAdapter adapter, ListView listview, int singleOffer){
Bitmap bigbitmap;
try{
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<>();
// If screenshot from single offer, than the item with the shop info has to be added too
OfferItem singleOfferItem = adapter.getItem(singleOffer);
OfferItem item;
for (int i = 0; i < itemscount; i++) {
item = adapter.getItem(i);
if (((singleOffer == 0) || (i == singleOffer)) ||
((singleOffer != 0) && (item.getShopID() == singleOfferItem.getShopID()) && item.getShopLogoID() != 0)){
View childView = adapter.getView(i, null, listview);
childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.destroyDrawingCache();
childView.buildDrawingCache();
bmps.add(childView.getDrawingCache(true));
allitemsheight += childView.getMeasuredHeight();
}
}
bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight+=bmp.getHeight();
bmp.recycle();
bmp=null;
}
for (int i = 0; i < itemscount; i++) {
View childView = adapter.getView(i, null, listview);
childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.destroyDrawingCache();
}
}catch (Exception e){
bigbitmap = null;
}
return bigbitmap;
}
第300行是:
bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
答案 0 :(得分:0)
此代码与使用ListView的整个想法背道而驰。 listView的重点是你不能为所有孩子获得视图 - 你只创建(并测量和绘制)实际在屏幕上的孩子。这样做的原因是为了保存大型列表所需的大量内存。
基本上如果你直接调用getView-你做错了。如果你曾经在一个循环中调用getView-你不仅做错了,而且你正在抵制列表视图,并且比你刚开始制作一个巨大的视图时更糟糕。
此外 - 如果您的列表足够大,可以占用超过一个完整的屏幕,那将是巨大的。位图占用4 *宽*高度字节的内存(加上一些开销)。如果你在现代密度设备上有2个屏幕长,你可以很容易地占用10s或100s megs。你只是没有那么多记忆。
此代码永远不会工作,如果大小超过一个微不足道的数量,它将无法工作。我首先会质疑为什么你认为你需要视图中每个项目的位图(而不仅仅是屏幕上的内容),因为这不太可能是你真正想要的,或者根本不可能实现的。