解决安卓7.0系统调用URI,软件直接闪退问题

解决安卓7.0系统调用URI,软件直接闪退问题

【注:学习Android做的笔记,大神勿喷。有不足之处还望不吝赐教。注释得很明白了,就不多去解释了】

最近开发APP,需要调用系统的分享图片功能拍照,代码都是通用的,但放到7.0系统以上的手机上却直接闪退报错,后来查出了问题(exposed beyond app through ClipData.Item.getUri())。
看到了这下面位老哥同样的问题,我也试一下,发现问题都解决了。
安卓7.0以上的手机拍照报错exposed beyond app through ClipData.Item.getUri()

解决问题需要已添加这三行代码到onCreate()方法中

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();

顺便贴出调测过,可用的代码(分享单张、多张图片和文字)

分享单张图片代码

//获取指定文件,可以理解获取文件所在路径
File imagePath = Environment.getExternalStoragePublicDirectory("/黑程序/Rcode/1.png");
// 由文件得到uri
Uri imageUri = Uri.fromFile(imagePath);
//调试打印日志
Log.d("share111111", "uri:" + imageUri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享到"));

分享多张图片代码

//获取指定文件,可以理解获取文件所在路径
File imagePath = Environment.getExternalStoragePublicDirectory("/黑程序/Rcode/1.png");
// 由文件得到uri
Uri imgUri = Uri.fromFile(imagePath);
//创建多图List
ArrayList<Uri> imgUris = new ArrayList<Uri>();
imgUris.add(imgUri);  //其中imgUri1为第一张图片的标识符
imgUris.add(imgUri); //其中imgUri2为第二张图片的标识符

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("image/*");
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUris);
shareIntent = Intent.createChooser(shareIntent, "分享到");
startActivity(shareIntent);

分享文字代码

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my Share text.");
//设置分享列表的标题,并且每次都显示分享列表
startActivity(Intent.createChooser(shareIntent, "分享到"));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辛辞།

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值