-1

So my problem is the output of this code is this (the square one) instead of what I should expect. Any ideas why this happens?

I'll explain this much further, the output is basically a square like image but not really square but it contains all the view but it is compressed unlike what we should expect, a fullscreen size image with all the views in the proper place:

enter image description here

Code:

private Bitmap getScreenshot(int id) {
    Log.d(TAG, "Creating screenshot");
    RelativeLayout RL= (RelativeLayout)findViewById(id);
    View v1 = getWindow().getDecorView().getRootView(); //or RL.getRootView();

    v1.setDrawingCacheEnabled(true);

    v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                  MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight()); 

    v1.buildDrawingCache(true);
    Bitmap bm = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    return bm;
}

Image:

enter image description here

Blauharley
  • 4,186
  • 6
  • 28
  • 47
  • I can't add a new link(I need 10 reputations). I am solely desperate admins. Link: https://www.pixeldroidof.net/qcode.txt – Pixeldroid Modding Feb 18 '17 at 10:35
  • http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android/5651242#5651242 please check this two link http://stackoverflow.com/questions/32624133/how-to-take-a-screen-shot-on-a-button-click-can-anyone-provide-a-android-code/32624305#32624305 – Zaki Pathan Feb 18 '17 at 10:48
  • If you need screenshot without title bar check this http://stackoverflow.com/a/30212385/7320259 – Zaki Pathan Feb 18 '17 at 10:50
  • http://stackoverflow.com/questions/42313304/if-i-take-screenshot-the-screen-shot-image-show-in-my-activity-how-to-hide-it?noredirect=1#comment71782188_42313304 this code works fine – Zaki Pathan Feb 18 '17 at 10:52

1 Answers1

0

The answer to this question is: Just force the width and the height of the views. If it is auto scaled like match_parent, the screenshot will not see it like how it is outputted. Code: view.getLayoutParams().width = width;

Same goes with the height https://i.stack.imgur.com/mFuNG.png