欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-11 13:32:29
解决android调用系统分享图片给微信,出现分享失败,分享多文件必须为图片格式近期应公司需求,分享多图片到微信的功能,之前一直是用微信自己家sdk实现分享,但是查看微信的原生sdk是不具备多图分享的...

解决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<uri> imageuris = new arraylist<uri>();
    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报错问题

<application
    android:allowbackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name1"
    android:supportsrtl="true"
    android:theme="@style/apptheme">

    <!--    //临时访问文件的注册-->
    <provider
      android:name="android.support.v4.content.fileprovider"
      android:authorities="com.lipuwulian.blesample.provider"//这是自己的包名加“.provider”
      android:exported="false"
      android:granturipermissions="true">
      <meta-data
        android:name="android.support.file_provider_paths"
        android:resource="@xml/file_paths" />
    </provider>

    <activity android:name="com.lipuwulian.blesample.mainactivity">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
   
  </application>

在res文件夹下创建xml文件夹、

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

file_paths文件里的内容:path是data/包名加.testapplication/

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <paths>
    <external-path
      name="files_root"
      path="android/data/com.lipuwulian.blesample.testapplication/" />
    <external-path
      name="external_storage_root"
      path="." />
  </paths>
</resources>

到这里就结束了,希望能够帮到大家哦!it需要爱与和平????

到此这篇关于解决android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式的问题的文章就介绍到这了,更多相关android 调用系统分享给微信内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!