Tuesday, March 24, 2015

Screenshot of ListView in Android Application

Standard
Hello All:

Sometimes in our application we need to save layout containing ListView as image so that it can be shared or saved somewhere.
In this post I will share the code which saves a ListView with Header view as bitmap.

Following helper method can be used to save listview to an image.
 
 public static <T extends BaseAdapter> String getBitmapPath(ListView view, T adapter) {
        try {
            view.setDrawingCacheEnabled(true);
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            view.buildDrawingCache(true);
            List<Bitmap> bitmaps = new ArrayList<Bitmap>();
            int count = adapter.getCount();
            bitmaps.add(view.getDrawingCache());
            int fullHeight = view.getHeight();
            for (int index = 0; index < count; index++) {
                View childView = adapter.getView(index, null, view);
                childView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
                childView.setDrawingCacheEnabled(true);
                childView.buildDrawingCache(true);
                bitmaps.add(childView.getDrawingCache());
                fullHeight += childView.getMeasuredHeight();
            }
            Bitmap bitmapToSave = Bitmap.createBitmap(view.getWidth(), fullHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmapToSave);
            canvas.drawColor(Color.WHITE);
            Paint paint = new Paint();
            int height = 0;

            for (Bitmap bitmap : bitmaps) {
                canvas.drawBitmap(bitmap, 0, height, paint);
                height += bitmap.getHeight();
                bitmap.recycle();
            }
            ///To support older devices
            for (int index = 0; index < count; index++) {
                View childView = adapter.getView(index, null, view);
                childView.setDrawingCacheEnabled(false);
            }
            bitmaps.clear();
            File file, outputFile;
            file = new File(android.os.Environment.getExternalStorageDirectory(), "cacheFolder");
            if (!file.exists()) {
                file.mkdirs();
            }
            String path = file.getAbsolutePath() + File.separator + "listview" + ".png";
            outputFile = new File(path);
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            bitmapToSave.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
            outputStream.close();
            view.setDrawingCacheEnabled(false);
            return path;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
This helper method can be used save listview as an image.
Hope this helps. Happy coding :)
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

0 comments :

Post a Comment