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