android 截取当前屏幕保存到sd卡中

来源:互联网 发布:空气湿度计算软件 编辑:程序博客网 时间:2024/05/21 06:43
 /**     * @return SD卡路径     */    private String getSDCardPath(){        File sdcardDir = null;        //判断SDCard是否存在        boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);        if(sdcardExist){            sdcardDir = Environment.getExternalStorageDirectory();        }        return sdcardDir.toString();    }



/** * 截取屏幕图片 */private void GetAndSaveCurrentScreen(){          //1.构建Bitmap          WindowManager windowManager = getWindowManager();          Display display = windowManager.getDefaultDisplay();          int w = display.getWidth();          int h = display.getHeight();          Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );              //2.获取屏幕          View decorview = this.getWindow().getDecorView();           decorview.setDrawingCacheEnabled(true);           Bmp = decorview.getDrawingCache();           String SavePath = getSDCardPath()+"/JmWeather/SharePicture";        //3.保存Bitmap           try {              File path = new File(SavePath);              //文件              sharePicPath = SavePath + "/share.png";              File file = new File(sharePicPath);              if(!path.exists()){                  path.mkdirs();              }              if (!file.exists()) {                  file.createNewFile();              }              FileOutputStream fos = null;              fos = new FileOutputStream(file);              if (null != fos) {                  Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);                  fos.flush();                  fos.close();                }          } catch (Exception e) {              e.printStackTrace();          }      }  


0 0
原创粉丝点击