GridView用于加载图像和错误java.lang.outofmemoryerror

时间:2014-11-07 20:38:22

标签: gridview out-of-memory

我是android编程的新手。我有一个gridView布局来显示png文件。但是当GridActivity启动时,应用程序停止。 这是我的错误:java.lang.outofmemoryerror 注意:当我只使用15个png文件时,我的程序没有崩溃。但是有40-50个文件,我的应用程序崩溃了。我怎么能解决这个问题?

public class Frames extends Activity {

    public Integer[] myImages = {
                              R.drawable.frame1,
                              R.drawable.frame2,
                              R.drawable.frame3,
                              R.drawable.frame4,
                              R.drawable.frame5,
                              R.drawable.frame6,
                              R.drawable.frame7,
                              R.drawable.frame8,
                              R.drawable.frame9,
                              R.drawable.frame10,
                              R.drawable.frame11,
                              R.drawable.frame12,
                              R.drawable.frame13,
                              R.drawable.frame14,
                              R.drawable.frame15,
                              R.drawable.frame16,
                              R.drawable.frame17,
                              R.drawable.frame18,
                              R.drawable.frame19,
                              R.drawable.frame20,
                              R.drawable.frame21,
                              R.drawable.frame22,
                              R.drawable.frame23,
                              R.drawable.frame24,
                              R.drawable.frame25,
                              R.drawable.frame26,
                              R.drawable.frame27,
                              R.drawable.frame28,
                              R.drawable.frame29,
                              R.drawable.frame30,
                              R.drawable.frame31,
                              R.drawable.frame32,
                              R.drawable.frame33,
                              R.drawable.frame34,
                              R.drawable.frame35,
                              R.drawable.frame36,
                              R.drawable.frame37,
                              R.drawable.frame38,
                              R.drawable.frame39,
                              R.drawable.frame40,
                              R.drawable.frame41,
                              R.drawable.frame42,
                              R.drawable.frame43,
                              R.drawable.frame44,
                              R.drawable.frame45,
                              R.drawable.frame46,
                              R.drawable.frame47,
                              R.drawable.frame48,
                              R.drawable.frame49,
                              R.drawable.frame50,
                              R.drawable.frame51,
                              R.drawable.frame52,
                              R.drawable.frame53,
                              R.drawable.frame54,
                              R.drawable.frame55,
                              R.drawable.frame56,

                              };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frames);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                //  String position_string = "you pressed the item number " + String.valueOf(position + 1) + " in GridView";
                //  Toast.makeText(getApplicationContext(), position_string, Toast.LENGTH_SHORT).show();

                Intent i = new Intent(getApplicationContext(), ArusiActivity.class);
                String Frame_Num = String.valueOf(position + 1);
                i.putExtra("frame_num", Frame_Num);
                setResult(RESULT_OK, i);
                finish();
                overridePendingTransition(R.drawable.slide_in, R.drawable.slide_out);

            }
        });
    }


    public static int convertDpToPixels(float dp, Context context) {
        Resources resources = context.getResources();
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                resources.getDisplayMetrics()
                );
    }


    public class ImageAdapter extends BaseAdapter {

        private Context mContext;


        @Override
        public int getCount() {
            return myImages.length;
        }


        @Override
        public Object getItem(int position) {
            return myImages[position];
        }


        @Override
        public long getItemId(int position) {
            return 0;
        }


        public ImageAdapter(Context c) {
            mContext = c;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                float w = 100;
                float h = 100;
                int width_pixel = convertDpToPixels(w, Frames.this);
                int height_pixel = convertDpToPixels(h, Frames.this);
                imageView.setLayoutParams(new GridView.LayoutParams(width_pixel, height_pixel));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
                imageView.setMaxHeight(60);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setBackgroundResource(myImages[position]);
            return imageView;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

我有这个问题。我认为这个错误是由于内存问题而发生的。 我通过使用Adobe Photoshop缩小图像大小和像素来解决它。现在它在我的设备上正常工作。