读书人

Android 截屏并写下SD卡中

发布时间: 2012-09-21 15:47:26 作者: rapoo

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()); 


读书人网 >Android

热点推荐