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

        在最近项目测试中,今天发现以前一直正常使用的分享图片到微信朋友圈的功能突然用不了了,点击分享到朋友圈时,提示“获取资源失败,无法分享到朋友圈”,测试手机微信是最近一两天刚更新到了微信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以及版本上后面添加图片“+”消失了。

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

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要在 Android 应用程序中实现分享微信的功能,可以使用微信官方提供的 SDK,具体步骤如下: 1. 在微信开放平台注册开发者账号,并创建应用,获取 AppID。 2. 在应用的 build.gradle 文件中添加以下依赖: ``` implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' ``` 3. 在 AndroidManifest.xml 文件中添加以下代码: ``` <!-- 注册微信 SDK --> <activity android:name=".wxapi.WXEntryActivity" android:exported="true" android:launchMode="singleInstance" android:taskAffinity="${applicationId}" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <meta-data android:name="com.tencent.mm.sdk.openapi.IWXAPI_APPID" android:value="替换为你的 AppID" /> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="wx替换为你的 AppID" /> </intent-filter> ``` 4. 创建一个实现了 IWXAPIEventHandler 接口的 Activity,并在其中处理微信回调。 5. 在需要进行分享的地方,调用以下代码: ``` // 初始化微信 API IWXAPI api = WXAPIFactory.createWXAPI(context, "替换为你的 AppID", true); api.registerApp("替换为你的 AppID"); // 创建分享消息对象 WXMediaMessage message = new WXMediaMessage(); message.title = "分享标题"; message.description = "分享描述"; // 设置消息缩略图 Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.thumb); message.thumbData = Util.bmpToByteArray(thumb, true); // 创建网页对象 WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = "http://www.example.com/"; message.mediaObject = webpage; // 构造一个Req SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = message; req.scene = SendMessageToWX.Req.WXSceneSession; // 分享到会话 // 调用api接口,发送数据到微信 api.sendReq(req); ``` 其中,Util.bmpToByteArray() 是一个将 Bitmap 转换为 byte[] 的工具方法,可以自行实现。另外,buildTransaction() 方法是一个生成唯一标识符的工具方法,可以使用当前间戳等方式实现。 以上就是在 Android 应用程序中实现分享微信的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值