Android 实现简单截屏并保存为文件
直接上代码:
[java]
/**
* 截屏
* @param v 视图
* @param filePath 保存路径
*/
private void getScreenHot(View v, String filePath)
{
try
{
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
v.draw(canvas);
try
{
FileOutputStream fos = new FileOutputStream(filePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
}
catch (FileNotFoundException e)
{
throw new InvalidParameterException();
}
}
catch (Exception e)
{
Log.i("截屏", "内存不足!");
e.printStackTrace();
}
}
调用方法:
[java]
getScreenHot((View) getWindow().getDecorView(), "/sdcard/test1.png");
作者:AMinfo
补充:移动开发 , Android ,