android 系统分享的微信报错获取资源失败

项目场景:

andoird 调用系统分享-点击微信分享获取资源失败


问题描述:

andoird 调用系统分享-点击微信分享获取资源失败
调起系统分享的代码:

Intent intent = new Intent(Intent.ACTION_SEND);
            if (jsonObject.has("image") && !TextUtils.isEmpty(jsonObject.optString("image"))) {
                intent.setType("image/*");   //分享图片
                byte[] bytes = Base64.decode(jsonObject.optString("image"), Base64.NO_WRAP);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                if (bitmap != null) {
                    String fileUrl = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null);
                    if(TextUtils.isEmpty(fileUrl)){// 插入系统相册失败时保存文件 upd 2019-04-13 19:00:41
                        fileUrl = saveFile(bitmap,100);
                    }
                    final Uri fileUri = Uri.parse(fileUrl);
                    intent.putExtra(Intent.EXTRA_STREAM, fileUri);  //传输图片或者文件 采用流的方式
                }

            } else {
                intent.setType("text/plain");//分享文本
            }

            String content=null;

            if (jsonObject.has("title") && !TextUtils.isEmpty(jsonObject.optString("title"))) {
                intent.putExtra(Intent.EXTRA_TITLE, jsonObject.optString("title"));
                intent.putExtra(Intent.EXTRA_SUBJECT, jsonObject.optString("title"));
                content=jsonObject.optString("title");
            }

            if (jsonObject.has("url") && !TextUtils.isEmpty(jsonObject.optString("url"))) {
                content+=jsonObject.optString("url");
                intent.putExtra(Intent.EXTRA_TEXT,content);   //附带的说明信息
                intent.putExtra("Kdescription", content);
            }else{
                intent.putExtra("Kdescription",content);
            }

            startActivity(Intent.createChooser(intent, "分享到"));

原因分析:

这样写时调起系统分享,点击微信分享时会弹出资源获取失败的toast


解决方案:

这样写时调起系统分享,点击微信分享时会弹出资源获取失败的toast

解决方案为:添加两句代码
Intent最好加上这个flag,intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
在分享文本等的时候必须加上intent.putExtra(Intent.EXTRA_TEXT,content);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值