Android 无法以编程方式拍摄已启动应用的快照。将mDrawingCache获取为NULL

Android 无法以编程方式拍摄已启动应用的快照。将mDrawingCache获取为NULL,android,Android,我可以通过下面的代码拍摄Hello World应用程序的快照。” private void takeScreenshot() ``{ Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); try { // image naming and path to include sd card app

我可以通过下面的代码拍摄Hello World应用程序的快照。”

private void takeScreenshot() 
``{
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            openScreenshot(imageFile);
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
    }
我已经使用Intent(getLaunchIntent for package方法)启动了另一个应用程序(excel)。现在,我想拍摄已发布应用程序的屏幕截图。我尝试调用与上面相同的方法,并更改了视图,如下所示:

视图v1=findviewbyd(android.R.id.content)

但是当我试图通过 位图cac=v1.getDrawingCache()

我得到的cac为空。如何检索已启动应用程序的视图,使getDrawingCache不返回NULL

如何检索已启动应用程序的视图

你不能,因为明显的隐私和安全原因


欢迎您在获得用户许可的情况下使用媒体投影API在Android 5.0+上截图。

@McflyDroid:只能从您的开发机器上截图。谢谢,我可以使用uiautomator进行截图