栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决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 调用系统分享给微信内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153870.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号