收藏
回答

微信分享 PDF 文件时,无法拉起微信怎么办?

首先,Android 微信分享文件的 sdk 里面找不到分享文件的实例代码,希望官方能补上。

https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Share_and_Favorites/Android.html,这个链接只提到了文字、图片、音乐、视频、网页、小程序六种分享,对于文件分享,给了这样一段解释:  OpenSDK支持FileProvider方式分享文件到微信(https://developers.weixin.qq.com/community/develop/doc/0004886026c1a8402d2a040ee5b401),关键这个链接里面的代码不完整,到关键的地方就没了:

public void shareToWechat(Context context) {
    // ...
    
    String filePath = context.getExternalFilesDir(null) + "/shareData/test.png";
    // 该filePath对应于xml/file_provider_paths里的第一行配置:,因此才可被共享
  
    File file = new File(filePath);
    String contentPath = getFileUri(context, file);
  
    // 使用contentPath作为文件路径进行分享
    // ...
}

资源下载中心的完整代码 demo(https://res.wx.qq.com/op_res/dl_hmdniBHBL1Jh2zHwHaZ-FM8YE9hTH6H6U7NKwbshR0QSgb7vH5aL4zwhtGqEt),里面也没有关于文件分享模块的代码,对于初次了解文件分享到微信的开发者来说,不是很友好。

后来网上搜到了其他平台开发者提供的例子,加上我对官方 api 的理解,写了如下代码:

WXFileObject obj = new WXFileObject();
if (BDApplication.get().checkVersionValid() && checkAndroidNotBelowN()) {
    File file = new File(path);
    String contentPath = getFileUri(con, file);
    obj.filePath = contentPath;
    obj.fileData = WxUtil.fileToByte(contentPath);
} else {
    obj.filePath = path;
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = obj;
req.message = msg;
req.scene = 1;
api.sendReq(req);


无法拉起微信,一闪而过,obj.fileData = WxUtil.fileToByte(contentPath) 这句,content:// 类型的 Uri 文件无法转化成 byte 类型的数据,不知道这里该怎么处理?

最后一次编辑于  2020-08-17
回答关注问题邀请回答
收藏

1 个回答

  • 疯狂的小辣椒
    疯狂的小辣椒
    2020-08-19

    你好,分享失败建议根据回调的错误信息排查,另外,这里也不建议使用fileData来传文件数据,有限制大小。

    2020-08-19
    有用
    回复 3
    • Trace
      Trace
      2020-11-13
      public void shareToWechat(Context context) {
          // ...
          String filePath = context.getExternalFilesDir(null) + "/shareData/test.png";
          // 该filePath对应于xml/file_provider_paths里的第一行配置:,因此才可被共享
          File file = new File(filePath);
          String contentPath = getFileUri(context, file);
          // 使用contentPath作为文件路径进行分享
          // ...
      }
      https://developers.weixin.qq.com/community/develop/doc/0004886026c1a8402d2a040ee5b401?page=3#comment-list
      
      这个文档只写了一句“// 使用contentPath作为文件路径进行分享”。
      关键的实例代码没有贴出来。
      官方能补上相关的实例代码吗?
      2020-11-13
      回复
    • 婷婷子
      婷婷子
      2021-12-29
      请问一下,fileData来传文件数据,限制大小是多少呀
      2021-12-29
      回复
    • 婷婷子
      婷婷子
      2021-12-29回复Trace
      请问,问题解决了嘛?
      2021-12-29
      回复
登录 后发表内容
问题标签