Unable to take full screenshot of visible + invisible part of the layout in android

I want to save the screenshot of the whole scrollable layout but it only captures the part which is visible on the screen.

Here is my whole layout.

enter image description here

Here is the screenshot which is captured:

enter image description here

Here is the code for screenshot:

private void takeScreenshot() {
    View v1 = getWindow().getDecorView().getRootView();;
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    saveScreenshot(bitmap,"filename");
}

public void saveScreenshot(Bitmap bitmap,String name) {
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + name + ".jpg";
    File imageFile = new File(mPath);
    Msg.log(mPath);
    try {
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 answer

  • answered 2019-10-08 03:21 ChandraMouli Poreddy

    Use the below code for taking full scroll view screen shot

        Bitmap bitmap = getBitmapFromView(scrollview, scrollview.getChildAt(0).getHeight(), scrollview.getChildAt(0).getWidth());
    
    private Bitmap getBitmapFromView(View view, int height, int width) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return bitmap;
    }
    

    replace your code saveScreenshot(bitmap,"filename"); with output from the above code return bitmap;

    SAMPLE EXAMPLE :

    https://programmingcode4life.blogspot.com/2016/10/convert-layout-view-to-image-and-store.html