-1

I am working on a application in which I have used a GridView and every image in Gridview have a small description, and when we click on any image I am showing this description, i have tested across different devices, its working perfect for MDPI and HDPI devices but when I have tested it on XHDPI device(High Resolution Device) I am getting Out Of Memory error.

I am not able to understand what is the problem, here is my activity :-

GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(getApplicationContext()));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), TrafficDetails.class);
                // passing array index
                i.putExtra("id", position);

                startActivity(i);

            }
        });

and here is my ImageAdapter in which i am using images from my drawable

    public Integer[] mThumbIds = {
            R.drawable.pic_1, R.drawable.pic_2,
            R.drawable.pic_3, R.drawable.pic_4,
            R.drawable.pic_5, R.drawable.pic_6,
            R.drawable.pic_7, R.drawable.pic_8,
            R.drawable.pic_9, R.drawable.pic_10,
            R.drawable.pic_11, R.drawable.pic_12,
            R.drawable.pic_13, R.drawable.pic_14,
            R.drawable.pic_15,R.drawable.pic_16,
            R.drawable.pic_17,R.drawable.pic_18,
            R.drawable.pic_19,R.drawable.pic_20,
            R.drawable.pic_21,R.drawable.pic_22,
            R.drawable.pic_23,R.drawable.pic_24,
            R.drawable.pic_25,R.drawable.pic_26,
            R.drawable.pic_27,R.drawable.pic_28,
            R.drawable.pic_29,R.drawable.pic_30,
            R.drawable.pic_31,R.drawable.pic_32,
            R.drawable.pic_33,R.drawable.pic_34,
            R.drawable.pic_35,R.drawable.pic_36,
            R.drawable.pic_37,R.drawable.pic_38,
            R.drawable.pic_39,R.drawable.pic_40,
            R.drawable.pic_41,R.drawable.pic_42,
            R.drawable.pic_43,R.drawable.pic_44,
            R.drawable.pic_45,R.drawable.pic_46,
            R.drawable.pic_47,R.drawable.pic_48,
            R.drawable.pic_49,R.drawable.pic_50,
            R.drawable.pic_51,R.drawable.pic_52,
            R.drawable.pic_53,
    };

    public String[] str_ary = {

// Description of all images
..................

    };

    // 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);
        //  TextView txt = new TextView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new GridView.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        //    txt.setLayoutParams(new GridView.LayoutParams(500, 500));
        return imageView;

please help me to sort out this, any help would be appreciable.....

thanks

Salman Khan
  • 2,822
  • 5
  • 32
  • 53

4 Answers4

4

try out as below:

ImageView imageView = new ImageView(mContext);
//  TextView txt = new TextView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(
      new  GridView.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT));   
Bitmap m_d = BitmapFactory.decodeResource(mContext.getResources(), 
                                   mThumbIds[p_position]);
if (m_d != null)
{
     Bitmap resizedBitmap = Bitmap.createScaledBitmap(m_d, 100, 100, true);
     imageView.setImageBitmap(resizedBitmap);
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • 1
    `matrix.postScale` is used to reduce the scale size of image. But its not used in this code , so its not required here.. :) – GrIsHu Jul 07 '13 at 12:10
0

you can use this code for get bitmaps :

https://stackoverflow.com/a/823966/1168654

you can handle bitmap size using

final int REQUIRED_SIZE=70;

below link for get bitmap from drawable folder you have to combine both code and use it in you app.

https://stackoverflow.com/a/3035869/1168654

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

you should use to viewHolder in getView.

below is listview sample but can apply to GridView :)

The world of ListView

dmnlk
  • 2,995
  • 2
  • 25
  • 30
0

It's not your coding. Well it is but it's nothing wrong with syntax, it's the way you haven't thought out and planned it properly. Android allocates a certain amount of memory for your app to use during run time. Let's say 10MB of RAM (this depends on how much memory is currently being used) And let's say you are trying to load 10 images with a total of 15MB RAM needed. Android would usually try give your app that extra 5MB but it's not always guaranteed. At that point Android could only say 'Out of Memory' and your app simply crashes.

Please refer this.

JanithaR
  • 1,094
  • 2
  • 11
  • 23