微信分享图片改用FileProvider方式分享

This commit is contained in:
lyr
2021-09-16 11:43:54 +08:00
parent 67f931f9c8
commit 8871d12fa8

View File

@ -23,11 +23,13 @@ import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.gh.common.Base64ImageHolder;
import com.gh.common.constant.Config;
import com.gh.gamecenter.BuildConfig;
import com.gh.gamecenter.R;
import com.gh.gamecenter.WeiBoShareActivity;
import com.lightgame.utils.Utils;
@ -354,7 +356,7 @@ public class MessageShareUtils {
//官方分享
WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(mWriteBitmapPath + picName);
imgObj.setImagePath(getWechatImagePath());
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
@ -389,7 +391,7 @@ public class MessageShareUtils {
}
WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(mWriteBitmapPath + picName);
imgObj.setImagePath(getWechatImagePath());
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
@ -494,6 +496,22 @@ public class MessageShareUtils {
}
}
/**
* Android11需要使用FileProvider方式分享所以要判断是否能用FileProvider
* OpenSDK版本必须大于或等于 5.4.3 版本微信版本为7.0.13及以上Android版本7.0及以上
* 微信版本的判断方式:当且仅当通过 IWXAPI.getWXAppSupportAPI() 接口获取到的值 >= 0x27000D00才能支持FileProvider的方式进行分享
*/
private String getWechatImagePath() {
String path = mWriteBitmapPath + picName;
if (mIWXAPI.getWXAppSupportAPI() >= 0x27000D00 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, new File(mWriteBitmapPath + picName));
// 授权给微信访问路径
mContext.grantUriPermission("com.tencent.mm", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
path = uri.toString();
}
return path;
}
/**
* 注意,第二个入参需要是 file 本身而不是 folder
*/