使用下面方法即可:
//截取屏幕
public void screenShort() {
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
if (bitmap != null) {
try {
saveBitmap(bitmap);
} catch (Exception e) {
}
}
}
//保存图片
private void saveBitmap(Bitmap bmp) throws IOException {
File childFolder = Environment.getExternalStoragePublicDirectory(Environment
.DIRECTORY_PICTURES);
File imageFile = new File(childFolder.getAbsolutePath() + "/" + System.currentTimeMillis
() + ".jpg");
OutputStream fOut = new FileOutputStream(imageFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 60, fOut);//将bg输出至文件
fOut.flush();
fOut.close(); // do not forget to close the stream
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile
(imageFile)));
ToastUtils.showShort(getString(R.string.success));
}
记得添加权限~~~, 懒得抽出来,全都加上吧
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
如果拒绝了,下面是提示用户开启权限的文章: