微信分享 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 类型的数据,不知道这里该怎么处理?