欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android截屏代码,, public clas

来源: javaer 分享于  点击 44809 次 点评:96

Android截屏代码,, public clas


 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(); }}
相关栏目:

用户点评