Android-屏幕截图功能实现

1,644 views

在Sitemap应用中添加了地图截屏功能,核心代码如下。

  1. private void GetandSaveCurrentImage()
  2. {
  3.     //1.构建Bitmap
  4.     WindowManager windowManager = getWindowManager();
  5.     Display display = windowManager.getDefaultDisplay();
  6.     int w = display.getWidth();
  7.     int h = display.getHeight();
  8.     Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
  9.     //2.获取屏幕
  10.     View decorview = this.getWindow().getDecorView();
  11.     decorview.setDrawingCacheEnabled(true);
  12.     Bmp = decorview.getDrawingCache();
  13.     //3.保存Bitmap
  14.     try {
  15.         File path = new File(SavePATH);
  16.         //文件
  17.         String filepath = SavePATH + “/Screen_1.png”;
  18.         File file = new File(filepath);
  19.         if(!path.exists()){
  20.             path.mkdirs();
  21.         }
  22.         if (!file.exists()) {
  23.             file.createNewFile();
  24.         }
  25.         FileOutputStream fos = null;
  26.         fos = new FileOutputStream(file);
  27.         if (null != fos) {
  28.             Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
  29.             fos.flush();
  30.             fos.close();
  31.             Toast.makeText(SiteMap.this, “截屏文件已保存至SDCard/ADASiteMaps/ScreenImage/下”, Toast.LENGTH_LONG).show();
  32.         }
  33.     } catch (Exception e) {
  34.         e.printStackTrace();
  35.     }
  36. }

转载本站文章请注明,转载自:阿达基站路测的天空[http://blog.signalsitemap.com]

本文链接:Android-屏幕截图功能实现 | 阿达基站路测的天空

This entry was posted in Android. Bookmark the permalink.

Comments are closed.