java.lang.outofmemoryerror exception while trying to display images in gridview

馋奶兔 提交于 2019-12-02 09:30:46

What is Happening here is that,,, the app is going out of heap size.So you have to decode the image in such a way that it will fall under the heap size.Do this code in your adapter

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        if(convertView == null){
            gridView = new View(mcontext);

            gridView = inflater.inflate(R.layout.imageview,null);
            ImageView imageView =(ImageView)gridView.findViewById(R.id.imageView);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = false;
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inSampleSize = 3;
            options.inPurgeable = true;

            Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                    Imageid[position],options);


            holder.imageView.setImageBitmap(icon);
        }
            else {
                gridView = (View) convertView;
            }


        return gridView;
    }

The images you are using are big.
Have a look at this article
OR
Scale your images down. - reduce their size and quality.

Tejas

Try reducing the size of your images according to the individual grid sizes. Check the link. How to resize Image in Android?

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