0
我可以通过下面的代码拍摄Hello World应用程序的快照。'无法以编程方式拍摄启动的应用程序的快照。将mDrawingCache取为NULL
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();
}
}
我已经推出了采用意图另一个应用程序(Excel文件)(getLaunchIntent用于封装的方法)。现在我想截取已启动的应用程序的屏幕截图。我尝试调用与上面相同的方法,并更改视图如下:
View v1 = findViewByID(android.R.id.content);
但是,当我试图通过 位图CAC = v1.getDrawingCache()
我越来越CAC为NULL来检索图形缓存。我如何检索启动的应用程序的视图,以便getDrawingCache不返回NULL?
但你可以通过亚行没有任何权利... – McflyDroid
@McflyDroid:只从你的开发机器。 – CommonsWare
谢谢我可以使用uiautomator来做到这一点 – user1726441