Android截屏代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
 public class MainActivity extends Activity {
 Button mButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
         @Override
           public void onClick(View v) {
             GetandSaveCurrentImage();
          }
        });
    }
 protected void GetandSaveCurrentImage() {
      WindowManager widowManager = getWindowManager();
      Display display = widowManager.getDefaultDisplay();
      int w = display.getWidth();
      int h = display.getHeight();
   
      Bitmap Bmp = Bitmap.createBitmap(w,h,Config.ARGB_8888);
      View decorview = this.getWindow().getDecorView();
      decorview.setDrawingCacheEnabled(true);
      Bmp = decorview.getDrawingCache();
  try {
      String SavePath = getSDCardPath() + "/ScreenImage";
      File path = new File(SavePath);
      String filepath = SavePath + "/Screen_1.jpg";
      File file = new File(filepath);
      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();
         Log.i("LW", "截屏文件已保存至SDCard/ScreenImage/下");
       }
  } catch (Exception e) {
       e.printStackTrace();
  }
 }
 private String getSDCardPath() {
  File sdCardDir = null;
  boolean sdcardExit = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
  if(sdcardExit){
   sdCardDir = Environment.getExternalStorageDirectory();
  }
  return sdCardDir.toString();
 }
}

标签: isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:php 利用cookie记录访问次数

下一篇:php读取cookie示例代码