分享

Android 截屏并写入SD卡中

 leomuyi 2014-01-08

-----------截屏方法

  1. private Bitmap shot() {    
  2.     View views = getWindow().getDecorView();  
  3.     views.buildDrawingCache();  
  4.   
  5.     // 获取状态栏高度  
  6.     Rect frames = new Rect();  
  7.     views.getWindowVisibleDisplayFrame(frames);  
  8.     int statusBarHeights = frames.top;  
  9.     Display display = getWindowManager().getDefaultDisplay();  
  10.     int widths = display.getWidth();  
  11.     int heights = display.getHeight();  
  12.     //第一种方式       
  13.     views.layout(0, statusBarHeights,widths, heights - statusBarHeights);  
  14.     views.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息 ,两种方式都需要加上  
  15.     Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache());  
  16.     //第二种方式       
  17.     // 1、source 位图  2、X x坐标的第一个像素  3、Y y坐标的第一个像素  4、宽度的像素在每一行  5、高度的行数  
  18.     //Bitmap bmp = Bitmap.createBitmap(views.getDrawingCache(), 0, statusBarHeights,widths, heights - statusBarHeights);  
  19.     return bmp;    
  20. }  


------------保存到SD卡方法

  1. try {  
  2.     String status = Environment.getExternalStorageState();  
  3.     // 判斷SD卡是否存在  
  4.     if (status.equals(Environment.MEDIA_MOUNTED)) {  
  5.         File destDir = new File("文件夹名");  
  6.           
  7.         if (!destDir.exists()) {  
  8.             // 创建文件夾  
  9.             destDir.mkdirs();  
  10.         }  
  11.         File file = new File("图片名");  
  12.         // 判断文件夾是否存在  
  13.         if (file.exists()) {  
  14.             String pic_path ="文件夹名" +"图片名"+".png";  
  15.             FileOutputStream out = new FileOutputStream(pic_path);  
  16.             shot().compress(Bitmap.CompressFormat.PNG,100, out);  
  17.             out.flush();  
  18.             out.close();  
  19.         }  
  20.     }  
  21. } catch (FileNotFoundException e) {  
  22.     e.printStackTrace();  
  23. } catch (IOException e) {  
  24.     e.printStackTrace();  
  25. }  


 

--------把Bitmap转为Drawable 放进imageView中

  1. //Bitmap-->Drawable    
  2.               BitmapDrawable bd=new BitmapDrawable(shot());    
  3.               imageView.setBackgroundDrawable(bd);    
  4.               imageView.setImageBitmap(shot());   


 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多