如何截取整个活动的截图?

时间:2015-09-25 00:39:20

标签: android android-studio screenshot android-screen-support

以下代码可以正常工作。但是,它只会截取用户可以看到的任何项目的屏幕截图,如果应用程序在小屏幕上运行并且未显示文本视图(必须向上或向下滚动),截图不会显示textview。无论屏幕尺寸如何,我如何截取整个活动的屏幕截图?

public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/groceryrun.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}


private void shareImage() {
    Intent share = new Intent(Intent.ACTION_SEND);

    // If you want to share a png image only, you can do:
    // setType("image/png"); OR for jpeg: setType("image/jpeg");
    share.setType("image/*");

    // Make sure you put example png image named myImage.png in your
    // directory
    String imagePath = Environment.getExternalStorageDirectory()
            + "/groceryrun.png";

    File imageFileToShare = new File(imagePath);

    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(share, "Share This Deal With Your Friends!"));
}

1 个答案:

答案 0 :(得分:1)

这是因为你正在截取一个只是个孩子的View的截图。去找父母

Activity.getWindow().getDecorView()

现在拨打您的密码。

让我知道它是否有效