所以我正在寻找一种无需根的截图 我发现了这个问题stackoverflow question
回答这个问题的罗宾先生说 “你只能获得自己过程的屏幕截图”1-是的我想为自己的印刷机拍摄一个屏幕截图可能有人提供了一些不需要root的代码吗?
2-我的想法是做一个透明的活动,然后使用in app屏幕截图可能吗?
3-另一件事是他们无论如何都要拍摄没有根的屏幕截图而不是在前台?我在Play商店看过很多可以拍摄屏幕并且不需要root的应用程序?任何想法?
答案 0 :(得分:0)
试试这个:
public static Bitmap screenshot(final View view) {
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
view.buildDrawingCache();
if (view.getDrawingCache() == null) {
return null;
}
final Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
return screenshot;
}
答案 1 :(得分:0)
考虑到我们想要在点击按钮时截取屏幕截图,代码将如下所示:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
首先,我们应该检索当前视图层次结构中的最顶层视图,然后启用绘图缓存,然后调用getDrawingCache()
。
调用getDrawingCache();
将返回表示视图的位图,如果禁用缓存,则返回null,这就是为什么在调用setDrawingCacheEnabled(true);
之前getDrawingCache()
应设置为true的原因。
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() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(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);
}
}
由于图像保存在外部存储设备上,因此应将WRITE_EXTERNAL_STORAGE
权限添加到AndroidManifest文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />