1

How to save my grid full grid view items in one image view someone help me. In below code this is my ImageAdapter for grid view and here is bit is my gridview item and i want to save "mthum" bitmap array in my sdcard in image view

            public  class ImageAdapter extends BaseAdapter {
            private Context mContext;
            Bitmap bit = Bitmap.createScaledBitmap(S.Murge_Bitmap, 280,  280, false);
            public Bitmap[] mThum={bit,bit,bit,bit,bit,bit,bit,bit,bit,bit,bit,bit};
            public ImageAdapter(Context c) {
            mContext = c;
            }

            public int getCount() {
            return mThum.length;
            }

            public Object getItem(int position) {
            return mThum[position];
            }
            public long getItemId(int position) {
            return 0;
            }


            public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {   
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setPadding(1, 1, 1, 1);
            } else {
            imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(mThum[position]);
            return imageView;
            } 

}

1 Answers1

4

You can use this solution, but I think it's gonna give you only a Bitmap of displayed images :

Bitmap b = Bitmap.createBitmap(mGridView.getDrawingCache());

if you have an issue with this and can't get a bitmap, try this :

mGridView.setDrawingCacheEnabled(true); 
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
mGridView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
mGridView.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

mGridView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(mGridView.getDrawingCache());
mGridView.setDrawingCacheEnabled(false); // clear drawing cache

otherwise, if you want all your images from your "Adapter" you can construct you Bitmap like describe in this answer :

https://stackoverflow.com/a/15152221/1686472

EDIT : I didn't see you wanted to save the image on SD card, there is plenty of answer about that so you should take a look at them :

https://stackoverflow.com/a/15662384/1686472

Community
  • 1
  • 1
Andros
  • 4,069
  • 1
  • 22
  • 30
  • this is running i used this code in my activity thanks – ravi patidar Feb 11 '14 at 08:15
  • Please mark it as answered. Check the mark at the top left of my answer. – Andros Feb 11 '14 at 08:18
  • it gave me error NullPointerException at line Bitmap b = Bitmap.createBitmap(gridview.getDrawingCache()); – Palak Mar 04 '14 at 10:12
  • Well you can check that your gridview != null && gridview.getDrawingCache() != null . But you won't have your bitmap. You need to call this method after the gridview is rendered. – Andros Mar 04 '14 at 10:16
  • Thanks its working but i want to convert whole gridview images in one bitmap not only visible portion. Is it possible? – Palak Mar 04 '14 at 10:37