处理使用Intent分享图片到微信朋友圈时,在微信7.0版本出现“获取资源失败,无法分享到朋友圈”,导致分享失败的问题

        在最近项目测试中,今天发现以前一直正常使用的分享图片到微信朋友圈的功能突然用不了了,点击分享到朋友圈时,提示“获取资源失败,无法分享到朋友圈”,测试手机微信是最近一两天刚更新到了微信7.0,以前该功能接口一直使用正常,为了验证这一点,卸载最新版微信,安装回上一微信版本6.7.3,发现图片分享到朋友圈功能接口正常,很明显,微信7.0大版本发布,图片分享到朋友圈的规则也发生了变化,如下是项目中一直以来处理图片分享(多图片)到朋友圈的主要代码(在已安装微信的前提下):


 
 
  1. /**
  2. * 分享图片到微信朋友圈
  3. * @param bmp 分享的图片的Bitmap对象
  4. * @param content 分享内容
  5. */
  6. public void shareImageToWechat(Bitmap bmp, String content) {
  7. File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
  8. String fileName = "share";
  9. File appDir = new File(file, fileName);
  10. if (!appDir.exists()) {
  11. appDir.mkdirs();
  12. }
  13. fileName = System.currentTimeMillis() + ".jpg";
  14. File currentFile = new File(appDir, fileName);
  15. FileOutputStream fos = null;
  16. try {
  17. fos = new FileOutputStream(currentFile);
  18. bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  19. fos.flush();
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. if (fos != null) {
  27. fos.close();
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. ArrayList<Uri> uris = new ArrayList<>();
  34. Uri uri = null;
  35. try {
  36. ApplicationInfo applicationInfo = mContext.getApplicationInfo();
  37. int targetSDK = applicationInfo.targetSdkVersion;
  38. if (targetSDK >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  39. uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mContext.getContentResolver(), currentFile.getAbsolutePath(), fileName, null));
  40. } else {
  41. uri = Uri.fromFile( new File(currentFile.getPath()));
  42. }
  43. uris.add(uri);
  44. } catch (Exception ex) {
  45. }
  46. Intent intent = new Intent();
  47. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  48. ComponentName comp = new ComponentName(PACKAGE_NAME_WEI_XIN, "com.tencent.mm.ui.tools.ShareToTimeLineUI");
  49. intent.setComponent(comp);
  50. intent.setType( "image/*");
  51. intent.putExtra( "Kdescription", content);
  52. intent.setAction(Intent.ACTION_SEND_MULTIPLE);
  53. intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
  54. mContext.startActivity(intent);
  55. }

  排查--系统分享接口正常

       在对比使用了系统自带的图片分享到朋友圈功能发现,发现只分享一张图片时,使用系统自带的图片分享到朋友圈的功能在微信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版本(及以上版本)降级使用单图片处理分享,以下兼容处理后的主要代码:


 
 
  1. /**
  2. * 微信7.0版本号,兼容处理微信7.0版本分享到朋友圈不支持多图片的问题
  3. */
  4. private static final int VERSION_CODE_FOR_WEI_XIN_VER7 = 1380;
  5. /**
  6. * 微信包名
  7. */
  8. public static final String PACKAGE_NAME_WEI_XIN = "com.tencent.mm";
  9. /**
  10. * 分享图片到微信朋友圈
  11. * @param bmp 分享的图片的Bitmap对象
  12. * @param content 分享内容
  13. */
  14. public void shareImageToWechat(Bitmap bmp, String content) {
  15. File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
  16. String fileName = "share";
  17. File appDir = new File(file, fileName);
  18. if (!appDir.exists()) {
  19. appDir.mkdirs();
  20. }
  21. fileName = System.currentTimeMillis() + ".jpg";
  22. File currentFile = new File(appDir, fileName);
  23. FileOutputStream fos = null;
  24. try {
  25. fos = new FileOutputStream(currentFile);
  26. bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  27. fos.flush();
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } finally {
  33. try {
  34. if (fos != null) {
  35. fos.close();
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. ArrayList<Uri> uris = new ArrayList<>();
  42. Uri uri = null;
  43. try {
  44. ApplicationInfo applicationInfo = mContext.getApplicationInfo();
  45. int targetSDK = applicationInfo.targetSdkVersion;
  46. if (targetSDK >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  47. uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mContext.getContentResolver(), currentFile.getAbsolutePath(), fileName, null));
  48. } else {
  49. uri = Uri.fromFile( new File(currentFile.getPath()));
  50. }
  51. uris.add(uri);
  52. } catch (Exception ex) {
  53. }
  54. Intent intent = new Intent();
  55. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  56. ComponentName comp = new ComponentName(PACKAGE_NAME_WEI_XIN, "com.tencent.mm.ui.tools.ShareToTimeLineUI");
  57. intent.setComponent(comp);
  58. intent.setType( "image/*");
  59. intent.putExtra( "Kdescription", content);
  60. if (VersionUtil.getVersionCode(mContext,PACKAGE_NAME_WEI_XIN) < VERSION_CODE_FOR_WEI_XIN_VER7) {
  61. // 微信7.0以下版本
  62. intent.setAction(Intent.ACTION_SEND_MULTIPLE);
  63. intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
  64. } else {
  65. // 微信7.0及以上版本
  66. intent.setAction(Intent.ACTION_SEND);
  67. intent.putExtra(Intent.EXTRA_STREAM, uri);
  68. }
  69. mContext.startActivity(intent);
  70. }
  71. public class VersionUtil {
  72. /**
  73. * 获取制定包名应用的版本的versionCode
  74. * @param context
  75. * @param
  76. * @return
  77. */
  78. public static int getVersionCode(Context context,String packageName) {
  79. try {
  80. PackageManager manager = context.getPackageManager();
  81. PackageInfo info = manager.getPackageInfo(packageName, 0);
  82. int version = info.versionCode;
  83. return version;
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. return 0;
  87. }
  88. }
  89. }

        处理后,在微信7.0版本上,项目中单图片可以正常分享到朋友圈了,同时也保留了微信7.0以下版本的多图片处理功能,但由于是单图片分享,进入到微信朋友圈图片分享编辑界面后,在微信7.0以及版本上后面添加图片“+”消失了。

       以上是我个人的见解和处理,希望能帮到大家,有不足的地方或有更好方案的朋友可以提出来,一起学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值