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" />


你可能感兴趣的:(Android)