Android截屏代码详解编程语言

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11054.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论