需求是把网络图片地址是string类型,生成图片,分享到朋友圈,遇到的问题是,7.0之前没有问题分享朋友圈,顺便切上代码:
Intent intent = new Intent();
Uri uri = null;
try {
String authority = context.getPackageName() + ".FileProvider";
ComponentName componentName = new ComponentName("com.tencent.mm",
type == 0 ? "com.tencent.mm.ui.tools.ShareImgUI" : "com.tencent.mm.ui.tools.ShareToTimeLineUI");
intent.setComponent(componentName);
//插入多张图片 ACTION_SEND_MULTIPLE
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra("Kdescription", text);
//7.0以上需要添加临时读取权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//插入多张图片
// ArrayList<Uri> imageUris = new ArrayList<Uri>();
//插入系统相册
// Uri uri =Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(),null,null));
uri = FileProvider.getUriForFile(context, authority, file);
// imageUris.add(uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}
} catch (Exception e) {
e.printStackTrace();
}
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
看着应该差不多了。嗯,,再贴上一个写的比较全的别的博客包括qq分享等:
https://www.jianshu.com/p/9522e24713e1
以为完事了,可是还是没法解决,还是提示“获取资源失败,仅支持分享照片至朋友圈”,各种百度,一直都没有找到解决的方法,dug看uri也是有的,这就很奇怪了,有的说是微信访问不到外部储存目录,也尝试修改了保存地址还是不行,还是怪思路不清晰,没有找到根本方法,看到这个博客就明白了。顺便贴上地址:
Android7.0分享朋友圈FileProvider生成的Uri无法被识别
ε=(´ο`*)))唉,怎么没有想到fileprovider生成的uri地址,应用不能识别了,还真是没想到,可能是经验不足吧,要把uri地址转换一下。
最后再贴上代码:
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
Uri uri = null;
if (cursor != null) {
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
uri = Uri.withAppendedPath(baseUri, "" + id);
}
cursor.close();
}
if (uri == null) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
return uri;
}