logo资料库

解决Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式的问题.pdf

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
调用系统分享给微信,出现分享失败 解决解决Android调用系统分享给微信 须为图片格式的问题 须为图片格式的问题 解决Android调用系统分享图片给微信,出现分享失败,分享多文件必须为图片格式 出现分享失败,分享多文件必 分享多文件必 近期应公司需求,分享多图片到微信的功能,之前一直是用微信自己家SDK实现分享,但是查看微信的原生SDK是不具备多 图分享的。在网上查找解决办法,直接调用手机系统进行分享,进行系统分享时分享给QQ,微博等都可以,但分享微信时就 会出现分享失败,分享多文件必须为图片格式,看网上各路大神都各显神通都没解决具体问题,于是自己就总结出此篇文章为 后来者少踩些坑,让你更快完成公司交给你的任务,让产品经理对你刮目相看,话不多说直接上干货。 private void systemShareWeChat(int shareTag,String photoPath){ Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.share); File f = null; ComponentName comp1,comp; comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//调用系统分享给微信朋友 comp1 = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//调用系统分享给微信朋 友圈 try { //将Android中drawable图片保存到本地 String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"share"+".jpg"; f = new File(dir); if (!f.exists()) { f.getParentFile().mkdirs(); f.createNewFile(); } FileOutputStream out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.PNG, 80, out); out.flush(); out.close(); Uri uri = FileProvider.getUriForFile(NativePhoto.this.getApplicationContext(), "com.lipuwulian.blesample.provider", f);//这个是版本大于Android7.0(包含)临时访问文件,没有这个会报异常 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ArrayList imageUris = new ArrayList(); imageUris.add(UrigetImageContentUri(NativePhoto.this,new File(photoPath)));//这个是分享本地存储的图片 imageUris.add(UrigetImageContentUri(NativePhoto.this,f)); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); if(shareTag==0){ shareIntent.setComponent(comp1);//分享给微信朋友圈 }else if(shareTag==1){ shareIntent.setComponent(comp);//分享给微信朋友 } //如果去掉shareIntent.setComponent("*");系统会调出所有的分享软件 shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(shareIntent); } //如果是微信分享的话一定一定将这个直接复制到自己项目中,将自己图片路径换为content:不然就会出现上述错误 public static Uri UrigetImageContentUri(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; } 这样就解决了调用系统分享报出分享失败,分享多文件必须为图片格式的错误了。 在AndroidManifest中临时文件注册解决Android7.0版本及其之后文件uri报错问题 在res文件夹下创建xml文件夹、 file_paths文件里的内容:path是data/包名加.testapplication/
到这里就结束了,希望能够帮到大家哦!IT需要爱与和平 您可能感兴趣的文章:Android调用系统自带的分享功能实例代码Android 调用系统联系人界面(添加联系人,添加已有联系人, 您可能感兴趣的文章 编辑和修改)Android实现调用系统图库与相机设置头像并保存在本地及服务器Android分享微信小程序失败的一些事小结 Android实现分享微信好友及出现闪退的解决办法
分享到:
收藏