Android 屏幕截图或View截图(快照)并保存至相册

通过Android中View类提供的获取控件绘制缓存方法,获取某个View控件每次绘制后的快照,来达到截图效果
该方法获取“截图”无需申请权限,但是将截图保存至相册时则需要读写权限

1.屏幕截图

        //获取 Activity 的顶层 View 的绘制快照,相当于截取了整个 Activity 的屏幕
        //获取指定 activity 顶层 View
        View view = activity.getWindow().getDecorView();
        //设置 View 允许绘制缓存
        view.setDrawingCacheEnabled(true);
        //得到绘制缓存 Bitmap
        Bitmap bitmap = view.getDrawingCache();
        //保存截图
        //saveImageToGallery(bitmap, activity);

2.View截图

        //获取指定 View 的绘制快照,以达到 View 截图效果
        //设置 View 允许绘制缓存
        view.setDrawingCacheEnabled(true);
        //得到 View 的绘制缓存 Bitmap
        Bitmap bitmap = view.getDrawingCache();
        //保存截取的快照
        //saveImageToGallery(bitmap, context);

3.完整工具类


/**
 * Author : Ziwen Lan
 * Date : 2019/10/23
 * Time : 15:11
 * Introduction : 截屏工具类
 */
public class ScreenshotUtil {
 
    /**
     * 截取指定activity显示内容
     * 需要读写权限
     */
    public static void saveScreenshotFromActivity(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        saveImageToGallery(bitmap, activity);
        //回收资源
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
    }
 
    /**
     * 截取指定View显示内容
     * 需要读写权限
     */
    public static void saveScreenshotFromView(View view, Activity context) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        saveImageToGallery(bitmap, context);
        //回收资源
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
    }
 
    /**
     * 保存图片至相册
     * 需要读写权限
     */
    private static void saveImageToGallery(Bitmap bmp, Activity context) {
        File appDir = new File(getDCIM());
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + getDCIM())));
    }
 
    /**
     * 获取相册路径
     */
    private static String getDCIM() {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return "";
        }
        String path = Environment.getExternalStorageDirectory().getPath() + "/dcim/";
        if (new File(path).exists()) {
            return path;
        }
        path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/";
        File file = new File(path);
        if (!file.exists()) {
            if (!file.mkdirs()) {
                return "";
            }
        }
        return path;
    }
}

4.最后

APP的某些界面如果不希望用户能够截屏,可以对指定Activity设置安全标记

// 禁止截屏
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

设置该安全标记后,在该Activity界面,无论是系统截屏,还是adb命令获取截屏,都将无法使用(有root权限的不正常情况除外)

你可能感兴趣的:(Android 屏幕截图或View截图(快照)并保存至相册)