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

前端 未结 3 360
暖寄归人
暖寄归人 2021-01-27 14:03

I am trying to display 6 images, which i have kept in res/drawable folder in the gridview.

but i get the following error

10-29 09:44:20.025: E/AndroidRun         


        
相关标签:
3条回答
  • 2021-01-27 14:19

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

    0 讨论(0)
  • 2021-01-27 14:28

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

    0 讨论(0)
  • 2021-01-27 14:38

    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;
        }
    
    0 讨论(0)
提交回复
热议问题