Android截屏代码
发布时间:2020-12-15 00:19:28 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class MainActivity extends Activity { Button mButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(save
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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(); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读