简体   繁体   中英

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.

在此处输入图像描述

Here is the screenshot which is captured:

在此处输入图像描述

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();
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM