Android 截屏并写入SD卡中
-----------截屏方法
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());