Android截取當前屏幕圖片並且保存到本地

截取屏幕


//截取屏幕
public void screenShort() {
    View dView = getWindow().getDecorView();
    dView.setDrawingCacheEnabled(true);
    dView.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
    if (bitmap != null) {
        try {
            saveBitmap(bitmap);
        } catch (Exception e) {
        }
    }
}


然後是保存到本地相冊

private void saveBitmap(Bitmap bmp) throws IOException {
    File childFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile = new File(childFolder.getAbsolutePath() + "/" + System.currentTimeMillis
            () + ".jpg");
    OutputStream fOut = new FileOutputStream(imageFile);
    bmp.compress(Bitmap.CompressFormat.JPEG, 60, fOut);
    fOut.flush();
    fOut.close(); 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));
}

最重要的是別忘了權限的問題


    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章