当我尝试从SD卡滚动查找更多图像时,它显示java.lang.OutOfMemoryError
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
at net.altnetwork.accountability.helpers.ScreenshotLibrary$LibraryItem.getBitmap(ScreenshotLibrary.java:124)
at net.altnetwork.accountability.helpers.ScreenshotLibrary$ItemGroup.getBitmaps(ScreenshotLibrary.java:102)
at net.altnetwork.accountability.v2.GalleryAdapter.getView(GalleryAdapter.java:127)
at android.widget.AbsListView.obtainView(AbsListView.java:2143)
at android.widget.ListView.makeAndAddView(ListView.java:1831)
at android.widget.ListView.fillSpecific(ListView.java:1320)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3348)
at android.view.View.dispatchTouchEvent(View.java:7246)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
at android.view.View.dispatchPointerEvent(View.java:7426)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
at java.lang.refl
Force finishing activity net.altnetwork.accountability/.activities.HomeActivity
这是课程文件...
public class GalleryAdapter extends BaseAdapter implements OnItemClickListener {
private Context mContext;
private int numcolumn;
private int NoOfListItem;
GridView grid;
int ListItemHeight;
private String[] header;
SharedPreferences groupbyPref;
private SharedPreferences GalleryPref;
ScreenshotLibrary myScreenshotLibrary = null;
public GalleryAdapter(Context gallery, int NumOfListItem, String[] str) {
SharedPreferences ImageSizePref = PreferenceManager
.getDefaultSharedPreferences(gallery);
numcolumn = ImageSizePref.getInt("NoOfColumn", 5);
GalleryPref = PreferenceManager.getDefaultSharedPreferences(gallery);
Editor edit = GalleryPref.edit();
edit.putInt("NumOfListItem", NumOfListItem);
edit.commit();
this.header = str;
this.mContext = gallery;
this.ListItemHeight = GalleryPref.getInt("ListHeight", 1000);
this.NoOfListItem = GalleryPref.getInt("NumOfListItem", 5);
groupbyPref = PreferenceManager.getDefaultSharedPreferences(gallery);
String key = groupbyPref.getString("GroupKey", "DAY");
if (key.equals("DAY")) {
myScreenshotLibrary = ScreenshotLibrary.getInstance(gallery,
ScreenshotLibrary.GroupingMethod.DAY);
Bitmap[] item = myScreenshotLibrary.getGroup(1).getBitmaps();
}
if (key.equals("WEEK")) {
myScreenshotLibrary = ScreenshotLibrary.getInstance(gallery,
ScreenshotLibrary.GroupingMethod.WEEK);
}
if (key.equals("MONTH")) {
myScreenshotLibrary = ScreenshotLibrary.getInstance(gallery,
ScreenshotLibrary.GroupingMethod.MONTH);
}
}
@Override
public int getCount() {
return NoOfListItem;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void setnumcol(int i) {
grid.setNumColumns(i);
grid.requestLayout();
notifyDataSetChanged();
}
public int ListItemHeight(int length) {
SharedPreferences ImageSizePref = PreferenceManager
.getDefaultSharedPreferences(mContext);
Log.i("listsize",
"" + ((12 / numcolumn) + 1)
* ImageSizePref.getInt("ImageParm", 100));
Log.i("listsizen", "" + ((12 / numcolumn) + 1));
Log.i("listsizei", "" + 12 % 4);
if (length % numcolumn == 0) {
return ((length / numcolumn))
* ImageSizePref.getInt("ImageParm", 100) + 55;
} else {
return ((length / numcolumn) + 1)
* ImageSizePref.getInt("ImageParm", 100) + 55;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View customRow = inflater.inflate(R.layout.list_item, null);
grid = (GridView) customRow.findViewById(R.id.gridView1);
TextView txt = (TextView) customRow.findViewById(R.id.header);
txt.setText(header[position]);
Bitmap[] item = myScreenshotLibrary.getGroup(position).getBitmaps();
grid.setAdapter(new gridadt(mContext, item));
grid.setNumColumns(numcolumn);
grid.requestLayout();
notifyDataSetChanged();
grid.setVerticalScrollBarEnabled(false);
customRow
.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.MATCH_PARENT,
ListItemHeight(item.length)));
Log.i("itemHeight", "" + customRow.getLayoutParams().height);
customRow.requestLayout();
grid.setOnItemClickListener(this);
return customRow;
}
public class gridadt extends BaseAdapter {
Context mcontext;
LinearLayout.LayoutParams parms;
private int GroupCount;
ScreenshotLibrary myScreenshotLibrary = null;
private int count;
Bitmap[] images;
LayoutInflater inflater;
public gridadt(Context adapter, Bitmap[] item) {
// inflater = ((Activity) mcontext).getLayoutInflater();
inflater = (LayoutInflater) adapter
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mcontext = adapter;
this.images = item;
this.count = item.length;
SharedPreferences ImageSizePref = PreferenceManager
.getDefaultSharedPreferences(adapter);
int size = ImageSizePref.getInt("ImageParm", 100);
parms = new LinearLayout.LayoutParams(size, size);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder {
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
/****** Inflate grid_item.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.grid_item, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.image = (ImageView) vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
ImageView image = holder.image;
// image.setImageBitmap(images[position]);
image.setImageResource(R.drawable.a);
image.setAdjustViewBounds(true);
image.setLayoutParams(parms);
return vi;
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
这是用于获取图像的功能。
public class LibraryItem{
public Bitmap getBitmap() throws FileNotFoundException{
InputStream is = context.getResources().openRawResource(R.drawable.gallery_thumb_img1);
return BitmapFactory.decodeStream(is);
}
}
最佳答案
遍历代码并准确查看其发生位置有些困难,但是您的问题肯定是您获得了对太多图像的引用,而没有将它们中的任何一个用于垃圾收集。然后,当android设备不再具有足够的内存来支持所有图像时,您将收到OOM异常。
在库适配器行127中,调用“ getBitmaps”,然后在new grdadt
中进行设置。确保在代码中的某个位置完全取消引用new grdadt
(例如,当它滚动到视图外时),并且应该允许垃圾回收正常工作。