截图方法 :
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);
long currenttime = System.currentTimeMillis();
String filepath = SavePath +File.separator+ currenttime+".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();
}