调用的系统自带的分享而不是接入的第三方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 需要进行适配