Android 截屏并写入SD卡中
2012-09-14 15:09:14 来源:WEB开发网核心提示:截屏方法private Bitmap shot() { View views = getWindow().getDecorView();views.buildDrawingCache();// 获取状态栏高度Rect frames = new Rect();views.getWindowVisibleDisplayF
截屏方法
private Bitmap shot() {
View views = getWindow().getDecorView();
views.buildDrawingCache();
// 获取状态栏高度
Rect frames = new Rect();
views.getWindowVisibleDisplayFrame(frames);
int statusBarHeights = frames.top;
Display display = getWindowManager().getDefaultDisplay();
int widths = display.getWidth();
int heights = display.getHeight();
//第一种方式
views.layout(0, statusBarHeights,widths, heights - statusBarHeights);
views.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息 ,两种方式都需要加上
Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache());
//第二种方式
// 1、source 位图 2、X x坐标的第一个像素 3、Y y坐标的第一个像素 4、宽度的像素在每一行 5、高度的行数
//Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights - statusBarHeights);
return bmp;
}
保存到SD卡方法
try {
String status = Environment.getExternalStorageState();
// 判斷SD卡是否存在
if (status.equals(Environment.MEDIA_MOUNTED)) {
File destDir = new File("文件夹名");
if (!destDir.exists()) {
// 创建文件夾
destDir.mkdirs();
}
File file = new File("图片名");
// 判断文件夾是否存在
if (file.exists()) {
String pic_path ="文件夹名" +"图片名"+".png";
FileOutputStream out = new FileOutputStream(pic_path);
shot().compress(Bitmap.CompressFormat.PNG,100, out);
out.flush();
out.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
把Bitmap转为Drawable 放进imageView中
//Bitmap-->Drawable
BitmapDrawable bd=new BitmapDrawable(shot());
imageView.setBackgroundDrawable(bd);
imageView.setImageBitmap(shot());
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接
