截图实现:
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = ScreenUtils.getScreenWidth(activity);
int height = ScreenUtils.getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width,
height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
保存bitmap格式的图片至存储卡。
public void savePic(Bitmap bitmap) {
//时间格式
final String FORMAT_PICTURE_NAME = "yyyyMMdd_HHmmss";
// 将时间作为pic的Name
String picName =
TimeUtil.getCurrentTime(FORMAT_PICTURE_NAME);
picName = "/IMG_" + picName + ".jpg";
// 使用此流读取
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 第二个参数影响的是图片的质量,但是图片的宽度与高度是不会受影响滴
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
// 这个函数能够设定图片的宽度与高度
// Bitmap map = Bitmap.createScaledBitmap(bitmap, 400, 400,
true);
// 把数据转为为字节数组
byte[] byteArray = baos.toByteArray();
try {
// 获取输入路径
FileOutputStream fos = new FileOutputStream(Environment
.getExternalStorageDirectory().getAbsolutePath() + picName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//向file中写数据
bos.write(byteArray);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
禁止截图:
注意:
截图是防止外部截取本activity的图,不是防止自己软件内部截自己的图。有点绕。
你要是在activity中实现了截图的方法,你又在这个页面开启了防截图,结果:
activity中截图方法依旧可以截图,而手机自带的截图软件则不能截图。