我需要在Android上截取整个屏幕的截图,我已经搜索了很多但他们都在谈论如何截取指定视图的截图,那么我该如何截取整个屏幕的截图呢?
我的意思是,通过程序实现。(而不是通过DDMS)
我需要在Android上截取整个屏幕的截图,我已经搜索了很多但他们都在谈论如何截取指定视图的截图,那么我该如何截取整个屏幕的截图呢?
我的意思是,通过程序实现。(而不是通过DDMS)
这段代码返回布局可见和不可见部分的屏幕截图。
private Bitmap getScreenBitmap() {
View v = getWindow().getDecorView().findViewById(android.R.id.content);
v.setDrawingCacheEnabled(true);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(measureSpec, measureSpec);
width = v.getMeasuredWidth();
height = v.getMeasuredHeight();
v.layout(0, 0, width, height);
v.buildDrawingCache(true);
//Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
v.draw(canvas);
v.setDrawingCacheEnabled(false);
return b;
}
尝试以下代码:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请参考此答案。
public void screenShot() throws InterruptedException
{
Process sh;
try
{
sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/Image.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}