<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
private void screenshot() { // 获取屏幕 View dView = getWindow().getDecorView(); dView.setDrawingCacheEnabled(true); dView.buildDrawingCache(); Bitmap bmp = dView.getDrawingCache(); if (bmp != null) { try { // 获取内置SD卡路径 String sdCardPath = Environment.getExternalStorageDirectory().getPath(); // 图片文件路径 String filePath = sdCardPath + File.separator + "screenshot.png"; File file = new File(filePath); FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); } catch (Exception e) { } } }
// 这里是截取状态栏
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.activity_main, menu); return true; }
private void screenshot1(String Strname)
{
/**
* Strname--如 2.png 需要带后缀
* */
// 获取屏幕
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
if (bmp != null)
{
try {
// 获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 图片文件路径
File filedir = new File(sdCardPath + File.separator + "AA"); // 这里的AA为创建的AA文件夹,在根目录下
if (!filedir.exists()) {
filedir.mkdirs();
}
File saveFile = new File(filedir, Strname); // aaaa.txt保存到AA文件夹下
FileOutputStream outStream1 = new FileOutputStream(saveFile);
System.out.println(outStream1.toString());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
outStream1.write(byteArray);
outStream1.close();
Toast.makeText(Zhuye_Activity.this,"截图保存成功",Toast.LENGTH_SHORT).show();
dView.setDrawingCacheEnabled(false); // 这里不设置false,那么下次截图还是上次的图片
} catch (Exception e) {
}
}
}