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;
    }
}

 

PS:       

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

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

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

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值