在最近项目测试中,今天发现以前一直正常使用的分享图片到微信朋友圈的功能突然用不了了,点击分享到朋友圈时,提示“获取资源失败,无法分享到朋友圈”,测试手机微信是最近一两天刚更新到了微信7.0,以前该功能接口一直使用正常,为了验证这一点,卸载最新版微信,安装回上一微信版本6.7.3,发现图片分享到朋友圈功能接口正常,很明显,微信7.0大版本发布,图片分享到朋友圈的规则也发生了变化,如下是项目中一直以来处理图片分享(多图片)到朋友圈的主要代码(在已安装微信的前提下):
-
/**
-
* 分享图片到微信朋友圈
-
* @param bmp 分享的图片的Bitmap对象
-
* @param content 分享内容
-
*/
-
public void shareImageToWechat(Bitmap bmp, String content) {
-
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
-
String fileName =
"share";
-
File appDir =
new File(file, fileName);
-
if (!appDir.exists()) {
-
appDir.mkdirs();
-
}
-
fileName = System.currentTimeMillis() +
".jpg";
-
File currentFile =
new File(appDir, fileName);
-
FileOutputStream fos =
null;
-
try {
-
fos =
new FileOutputStream(currentFile);
-
bmp.compress(Bitmap.CompressFormat.JPEG,
100, fos);
-
fos.flush();
-
}
catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
finally {
-
try {
-
if (fos !=
null) {
-
fos.close();
-
}
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
ArrayList<Uri> uris =
new ArrayList<>();
-
Uri uri =
null;
-
try {
-
ApplicationInfo applicationInfo = mContext.getApplicationInfo();
-
int targetSDK = applicationInfo.targetSdkVersion;
-
if (targetSDK >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-
uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mContext.getContentResolver(), currentFile.getAbsolutePath(), fileName,
null));
-
}
else {
-
uri = Uri.fromFile(
new File(currentFile.getPath()));
-
}
-
uris.add(uri);
-
}
catch (Exception ex) {
-
-
}
-
Intent intent =
new Intent();
-
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
-
ComponentName comp =
new ComponentName(PACKAGE_NAME_WEI_XIN,
"com.tencent.mm.ui.tools.ShareToTimeLineUI");
-
intent.setComponent(comp);
-
intent.setType(
"image/*");
-
intent.putExtra(
"Kdescription", content);
-
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
-
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
-
mContext.startActivity(intent);
-
}
排查--系统分享接口正常
在对比使用了系统自带的图片分享到朋友圈功能发现,发现只分享一张图片时,使用系统自带的图片分享到朋友圈的功能在微信7.0也是正常的,通过adb实时查看当前正在运行的Activity,项目中启动朋友圈图片分享Activity的Intent信息:

系统启动朋友圈图片分享的Intent信息:
可以发现,Intent的act以及flag信息有很大的区别,由于flags标志位涉及到较多的标志位组合,暂不排查,重点放在action字段上,对于微信朋友圈的图片分享,这两个action的含义如下:
android.intent.action.SEND_MULTIPLE:支持多张图片分享
android.intent.action.SEND:支持单张图片分享
进一步排查,不禁想,系统的分享支持多图片分享到朋友圈吗?
在系统相册上,只选择一张图片时,点击分享按钮,可以看到“发送到朋友圈”这个选项,按前面说的,图片也是可以正常分享的:

当选择两张及以上张数量的图片时,点击分享按钮,“发送到朋友圈”这个选项已经没有了,只有“发送给朋友”以及“添加到微信收藏”这两个微信相关的分享接口:

当自己把手机上的微信7.0版本卸载,重新装回微信6.7.3版本时,选择两张及以上张数量的图片,点击分享按钮,发现“发送到朋友圈”这个功能出现在了分享选择列表上!!!!!

从系统分享接口这一行为变化上,可以看出,个人猜测,对于使用原生Intent的方式,微信7.0版本进一步收紧了多图片分享到朋友圈的功能,对于图片分享,在微信7.0上只支持单图片分享。
在项目中降级解决
从上面分析可知,对于使用原生Intent的方式微信7.0版本不支持使用多图片分享到朋友圈,针对这种情况,对于微信7.0版本,做降级处理:在微信7.0版本(及以上版本)降级使用单图片处理分享,以下兼容处理后的主要代码:
-
/**
-
* 微信7.0版本号,兼容处理微信7.0版本分享到朋友圈不支持多图片的问题
-
*/
-
private
static
final
int VERSION_CODE_FOR_WEI_XIN_VER7 =
1380;
-
/**
-
* 微信包名
-
*/
-
public
static
final String PACKAGE_NAME_WEI_XIN =
"com.tencent.mm";
-
-
/**
-
* 分享图片到微信朋友圈
-
* @param bmp 分享的图片的Bitmap对象
-
* @param content 分享内容
-
*/
-
public void shareImageToWechat(Bitmap bmp, String content) {
-
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
-
String fileName =
"share";
-
File appDir =
new File(file, fileName);
-
if (!appDir.exists()) {
-
appDir.mkdirs();
-
}
-
fileName = System.currentTimeMillis() +
".jpg";
-
File currentFile =
new File(appDir, fileName);
-
FileOutputStream fos =
null;
-
try {
-
fos =
new FileOutputStream(currentFile);
-
bmp.compress(Bitmap.CompressFormat.JPEG,
100, fos);
-
fos.flush();
-
}
catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
finally {
-
try {
-
if (fos !=
null) {
-
fos.close();
-
}
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
ArrayList<Uri> uris =
new ArrayList<>();
-
Uri uri =
null;
-
try {
-
ApplicationInfo applicationInfo = mContext.getApplicationInfo();
-
int targetSDK = applicationInfo.targetSdkVersion;
-
if (targetSDK >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-
uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mContext.getContentResolver(), currentFile.getAbsolutePath(), fileName,
null));
-
}
else {
-
uri = Uri.fromFile(
new File(currentFile.getPath()));
-
}
-
uris.add(uri);
-
}
catch (Exception ex) {
-
-
}
-
Intent intent =
new Intent();
-
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
-
ComponentName comp =
new ComponentName(PACKAGE_NAME_WEI_XIN,
"com.tencent.mm.ui.tools.ShareToTimeLineUI");
-
intent.setComponent(comp);
-
intent.setType(
"image/*");
-
intent.putExtra(
"Kdescription", content);
-
if (VersionUtil.getVersionCode(mContext,PACKAGE_NAME_WEI_XIN) < VERSION_CODE_FOR_WEI_XIN_VER7) {
-
// 微信7.0以下版本
-
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
-
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
-
}
else {
-
// 微信7.0及以上版本
-
intent.setAction(Intent.ACTION_SEND);
-
intent.putExtra(Intent.EXTRA_STREAM, uri);
-
}
-
mContext.startActivity(intent);
-
}
-
-
-
-
-
public
class VersionUtil {
-
-
-
/**
-
* 获取制定包名应用的版本的versionCode
-
* @param context
-
* @param
-
* @return
-
*/
-
public static int getVersionCode(Context context,String packageName) {
-
try {
-
PackageManager manager = context.getPackageManager();
-
PackageInfo info = manager.getPackageInfo(packageName,
0);
-
int version = info.versionCode;
-
return version;
-
}
catch (Exception e) {
-
e.printStackTrace();
-
return
0;
-
}
-
}
-
-
}
处理后,在微信7.0版本上,项目中单图片可以正常分享到朋友圈了,同时也保留了微信7.0以下版本的多图片处理功能,但由于是单图片分享,进入到微信朋友圈图片分享编辑界面后,在微信7.0以及版本上后面添加图片“+”消失了。
以上是我个人的见解和处理,希望能帮到大家,有不足的地方或有更好方案的朋友可以提出来,一起学习。
微信7.0更新后,多图片分享至朋友圈功能失效。本文解析原因,提供兼容解决方案,确保不同版本微信的图片分享体验。
814

被折叠的 条评论
为什么被折叠?



