4

How do I take a screenshot of all items of a gridview.

I have a gridview with some images. I want to take a screenshot of the grid including all the items. I checked this question without any success. Any help will be appreciated.

Here is the code I have tried but it does not work.

1st way

private Bitmap createGrid()
    {
        layoutGrid.setDrawingCacheEnabled(true);
        layoutGrid.buildDrawingCache(true);
        Bitmap bmp = layoutGrid.getDrawingCache();
        if(bmp==null)
        {
            Log.i(getClass().getSimpleName(), "Bitmap is null");
        }
        else
            saveScreenShot(bmp);
        return bmp;

    }
    public void saveScreenShot(Bitmap bmp)
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File file = new File( Environment.getExternalStorageDirectory() +
                "/gridscreen.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            ostream.write(bytes.toByteArray());        
            ostream.close();
        } 
        catch(IOException ex)
        {

        }
    }

2nd way

public  Bitmap convertGridToBitmap() {

        GridView grid    = gridView;
        EgkGridViewAdapter adapter  = (EgkGridViewAdapter) grid.getAdapter(); 
        int itemscount       = adapter.getCount();
        int allitemsheight   = 0;
        List<Bitmap> bmps    = new ArrayList<Bitmap>();

        for (int i = 0; i < itemscount; i++) {

            View childView      = adapter.getView(i, null, grid);
            childView.measure(MeasureSpec.makeMeasureSpec(grid.getWidth(), MeasureSpec.EXACTLY), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            childView.buildDrawingCache();
            bmps.add(childView.getDrawingCache());
            allitemsheight+=childView.getMeasuredHeight();
        }

        Bitmap bigbitmap    = Bitmap.createBitmap(grid.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
        Canvas bigcanvas    = new Canvas(bigbitmap);

        Paint paint = new Paint();
        int iHeight = 0;

        for (int i = 0; i < bmps.size(); i++) {
            Bitmap bmp = bmps.get(i);
            bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
            iHeight+=bmp.getHeight();

            bmp.recycle();
            bmp=null;
        }


        return bigbitmap;
    }
Community
  • 1
  • 1
Ajeet
  • 143
  • 2
  • 10

0 Answers0