android 获取屏幕截图并调用系统分享

调用的系统自带的分享而不是接入的第三方sdk
第一步:
获取屏幕截图

 // 获取屏幕(包括导航
View dView =AddressDetailsActivity.this.getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        Bitmap bmp = dView.getDrawingCache();
//        // 获取屏幕(不包括导航
//        View dView = getWindow().getDecorView();
//        dView.setDrawingCacheEnabled(true);
//        dView.buildDrawingCache();
//        Bitmap bmp = Bitmap.createBitmap(dView.getDrawingCache());

        if (bmp != null) {
            try {
                // 获取内置SD卡路径
                String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                // 图片文件路径
                imagePath = sdCardPath + File.separator + "test.png";
                File file = new File(imagePath);
                FileOutputStream os = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
            } catch (Exception e) {
            }
        }

第二步:判断是否保存成功,成功即调用分享

        if (imagePath != null){
                    Intent intentz = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性
                    File file = new File(imagePath);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

                        Uri contentUri = FileProvider.getUriForFile(getApplication(), "xxxx.fileprovider", file);// xxx修改为自己项目的
                        intentz.putExtra(Intent.EXTRA_STREAM, contentUri);
                    }else {
                        intentz.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));// 分享的内容
                    }
                    intentz.setType("image/*");// 分享发送的数据类型
                    Intent chooser = Intent.createChooser(intentz, "Share screen shot");
                    if(intentz.resolveActivity(getPackageManager()) != null){
                        startActivity(chooser);
                    }

                }

ps:需要注意的也就是系统版本fileprovider 需要进行适配

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值