加载图片时出现java.lang.OutOfMemoryError

时间:2014-03-26 15:29:01

标签: java android

当我尝试从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);
    }
}

2 个答案:

答案 0 :(得分:0)

通过代码查看确切的位置有点困难,但是你的问题肯定是你在没有任何可用于垃圾收集的情况下获得太多图像的引用。然后,当Android设备不再有足够的内存来支持所有图像时,就会出现OOM异常。

在你的图书馆适配器行127中,你调用“getBitmaps”,然后在new grdadt中设置它。确保在代码中的某个位置(例如,当它滚出视图时)完全取消引用new grdadt,并且它应该允许垃圾收集正常工作。

答案 1 :(得分:0)

public View getView(int position, View convertView, ViewGroup parent) 

这个块对我来说似乎是你的问题,尤其是以下几行:

Bitmap[] item = myScreenshotLibrary.getGroup(position).getBitmaps();

似乎每次调用该函数时都会保存所有位图,如果我没有弄错,它会被调用很多。