通过intent和无障碍服务实现分享图片+文字到微信朋友圈

故事背景

在全民微商的大时代背景下,发圈是一个很重要的环节,基于这个需求,便出现了此篇文章

系统参数

硬件:小米手机6 MIUI9 安卓7.1.1
软件:微信6.6.7

预览

废话不多说,没图说个**

1533803880468975.gif

步骤

一、代码文件 WeiXinShareUtil

import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.pm.PackageInfo;  
import android.content.pm.PackageManager;  
import android.net.Uri;  
import android.widget.Toast;  

import java.io.File;  

/** 
 * Created by Administrator on 2015/6/24. 
 */  
public class WeiXinShareUtil {  
    public static void sharePhotoToWX(Context context, String text, String photoPath) {  
        if (!uninstallSoftware(context, "com.tencent.mm")) {  
            Toast.makeText(context, "微信没有安装!", Toast.LENGTH_SHORT).show();  
            return;  
        }  

        File file = new File(photoPath);  
        if (!file.exists()) {  
            String tip = "文件不存在";  
            Toast.makeText(context, tip + " path = " + photoPath, Toast.LENGTH_LONG).show();  
            return;  
        }  

        Intent intent = new Intent();  
        ComponentName componentName = new ComponentName("com.tencent.mm",  
                "com.tencent.mm.ui.tools.ShareToTimeLineUI");  
        intent.setComponent(componentName);  
        intent.setAction("android.intent.action.SEND");  
        intent.setType("image/*");  
        intent.putExtra("Kdescription", text);  
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));  
        context.startActivity(intent);  
    }  

    private static boolean uninstallSoftware(Context context, String packageName) {  
        PackageManager packageManager = context.getPackageManager();  
        try {  
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);  
            if (packageInfo != null) {  
                return true;  
            }  
        } catch (PackageManager.NameNotFoundException e) {  
            e.printStackTrace();  
        }  
        return false;  
    }  
}  

二、调用方式

WeiXinShareUtil.sharePhotoToWX(context, "test", photoPath);

第一个参数:上下文Context
第二次参数:你要分享的文字text
第三个参数:你要分享的图片路径photoPath

参考文章

关于7.0分享多图出现的问题,可以参考这篇文章:使用原生intent分享图片获取资源失败问题

三、 补充说明:
安卓微信6.6.7之后,微信对’Kdescription’做了处理,所有文字带不过去了。
image.png

四、微信 6.6.7不能传送文字的问题的解决方案
可以通过安卓的辅助服务功能,实现自动复制。就是监测微信发送朋友圈的页面->通过辅助服务把文字写入到Edittext。具体的代码已经放到下边的github仓库里了。

Github开源地址

  1. wxshareapp

先睹为快

  1. apk下载地址:github
欢迎关注我的个人公众号(捯饬捯饬啥)

daochidaochisha

猜你喜欢

转载自blog.csdn.net/yong531315/article/details/81538472