分享图片+文字到微信朋友圈

来源:互联网 发布:php游戏服务端开发 编辑:程序博客网 时间:2024/04/29 02:11

比较坑,昨天刚刚写好了用微信SDK Jar包分享的Dome,但是公司突然又要分享图片+文字,而且还不要网页式的分享,找了半天,发现直接用微信SDK无法做到这一点,所以就只能用Intent直接传值调用了。


好了,直接上代码,就是一个类:

一、代码文件 WeiXinShareUtil

<pre name="code" class="java">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


结束。如果有什么问题欢迎来讨论!

0 0