Android 程式碼實現螢幕截圖功能
阿新 • • 發佈:2019-01-04
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) { } } }