0

How to take screenshot for specific Layout

This code take screenshot for the the activity without the status bar i want to take screen for scroll view

View view = getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache(true);
            Bitmap b1 = view.getDrawingCache();
            Rect frame = new Rect();
            this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;
            int width = this.getWindowManager().getDefaultDisplay().getWidth();
            int height = this.getWindowManager().getDefaultDisplay()
                    .getHeight();

            Bitmap bm = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
                    height - statusBarHeight);

I want to take screenshot for the scroll view
i searched a lot here but i find nothing hope any one can help

mmsmhh
  • 15
  • 1
  • 6

2 Answers2

0

are you able to take the screenshot including the title bar ?

Then get the title bar height using this Link

DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int myHeight = 0;

    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_HIGH:
            Log.i("display", "high");
            myHeight = display.getHeight() - 48;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            Log.i("display", "medium/default");
            myHeight = display.getHeight() - 32;
            break;
        case DisplayMetrics.DENSITY_LOW:
            Log.i("display", "low");
            myHeight = display.getHeight() - 24;
            break;
        default:
            Log.i("display", "Unknown density");

Now in your code

replace

int height = this.getWindowManager().getDefaultDisplay()
                    .getHeight();

with

int height = this.getWindowManager().getDefaultDisplay()
                    .getHeight()-THE_HEIGHT_OF_NOTIFICATION_BAR;

i havent tried myself but this i guess would work see if it removes from top or bottom the image and do let us know wat happens...

thx

Community
  • 1
  • 1
Ahmad
  • 437
  • 1
  • 4
  • 12
  • It worksbut take a screenshot for the screen to to all the layout – mmsmhh Apr 25 '14 at 11:25
  • oh u mean the hidden ones also...?? like if my screen of 800dp while my screen height is 400dp so u want to cover full 800dp right..? – Ahmad Apr 25 '14 at 11:51
  • Yes that's what i want – mmsmhh Apr 25 '14 at 17:23
  • afaik android does not paint the hidden objects until they are displayed on the screen.. if i were to take screen shot of full scrollview i wud have used video capturing (https://code.google.com/p/androidscreencast/) instead to show exactly what is happening... and if only images were to be used then i would have splitted dat video into image (http://linuxers.org/tutorial/how-extract-images-video-using-ffmpeg) frame and combined them in single long image... hope it will help u solve ur issue... – Ahmad Apr 27 '14 at 12:35
0
public static Bitmap loadBitmapFromView(ScrollView v) {
    Bitmap b = Bitmap.createBitmap(v.getWidth() , v.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
}

Note: Pass your ScrollView instance to this method.