GridView example not working

北慕城南 提交于 2019-12-13 04:26:45

问题


I am taking help from this link: Android GridView Layout Tutorial. When I run my app my app stop working. I am getting following errors. I want to create gridview that show thumbnail images in gridview and full size image on click. For this I am saving thumbnail and full image of each image in drawable. How to do this? please help me.

ImageAdapter class-

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public Integer[] mThumbIds = {
            R.drawable.kri1, R.drawable.kri2,
            R.drawable.kri3, R.drawable.kri4,
            R.drawable.kri5, R.drawable.kri6,
            R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9,R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9, R.drawable.kri7, R.drawable.kri8,
            R.drawable.kri9
    };

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

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }

}

Logcat-

02-20 07:06:19.930: E/AndroidRuntime(2541): FATAL EXCEPTION: main
02-20 07:06:19.930: E/AndroidRuntime(2541): Process: com.example.gridview, PID: 2541
02-20 07:06:19.930: E/AndroidRuntime(2541): java.lang.OutOfMemoryError
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.content.res.Resources.loadDrawable(Resources.java:2110)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.content.res.Resources.getDrawable(Resources.java:700)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.widget.ImageView.resolveUri(ImageView.java:638)
02-20 07:06:19.930: E/AndroidRuntime(2541):     at android.widget.ImageView.setImageResource(ImageView.java:367)

回答1:


You are using high resolution images so it gives great load on heap. Instead try down sampling images you are using on getView().

BitmapFactory.Options options = new BitmapFactory.Options();
// set options for your need here
BitmapFactory.decodeResource(context, yourImageId, options);


来源:https://stackoverflow.com/questions/21953057/gridview-example-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!