分享本地视频到微信好友时出现分享受限的弹窗
//通过视频文件获取uri
public static String getFileUri(Context context, File file){
if (file == null || !file.exists()) {
return null;
}
// 要与`AndroidManifest.xml`里配置的`authorities`一致,假设你的应用包名为com.example.app
Uri contentUri = FileProvider.getUriForFile(context,
"com.yishi.cat.fileProvider",
file);
// 授权给微信访问路径
context.grantUriPermission("com.tencent.mm", // 这里填微信包名
contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
return contentUri.toString(); // contentUri.toString() 即是以"content://"开头的用于共享的路径
}
//清单文件 FileProvider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.yishi.cat.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
//file_paths文件内容
<?xml version="1.0" encoding="utf-8"?><!--<paths xmlns:android="http://schemas.android.com/apk/res/android">-->
<resources>
<paths>
<external-files-path
name="sharedata"
path="shareData/" />
<external-path
name="external_path"
path="." />
<root-path
name="root_path"
path="" />
</paths>
</resources>
//通过uri分享本地视频
int flag = 0;
if (scene == 1) {
//朋友圈
flag = SendMessageToWX.Req.WXSceneTimeline;
} else {
//好友
flag = SendMessageToWX.Req.WXSceneSession;
}
WXVideoFileObject fileObject = new WXVideoFileObject();
fileObject.filePath = uri;//获取的uri
fileObject.shareScene = scene;
//用 WXVideoObject 对象初始化一个 WXMediaMessage 对象
WXMediaMessage message = new WXMediaMessage(fileObject);
message.title = title;
message.description = description;
//构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("video");
req.message = message;
req.scene = flag;
//调用api接口,发送数据到微信
api.sendReq(req);
提供一下appid