1. 程式人生 > >Android 程式碼實現螢幕截圖功能

Android 程式碼實現螢幕截圖功能

private void screenshot()
{
    // 獲取螢幕
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
if (bmp != null)
    {
        try {
            // 獲取內建SD卡路徑
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 圖片檔案路徑 String filePath = sdCardPath + File.separator + "screenshot.png"; File file = new File(filePath); FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); } catch (Exception e) { } } }