时间:2021-05-20
用微信提供的SDK来实现分享:
从http://open.weixin.qq.com下载Android相关的jar包,将libammsdk.jar加入到项目中。
微信分享的核心类,部分代码如下:
WechatShareManager.java
package com.jackie.umeng.share; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.widget.Toast; import com.tencent.mm.sdk.modelmsg.SendMessageToWX; import com.tencent.mm.sdk.modelmsg.WXImageObject; import com.tencent.mm.sdk.modelmsg.WXMediaMessage; import com.tencent.mm.sdk.modelmsg.WXTextObject; import com.tencent.mm.sdk.modelmsg.WXVideoObject; import com.tencent.mm.sdk.modelmsg.WXWebpageObject; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.WXAPIFactory; /** * 实现微信分享功能的核心类 * @author chengcj1 * */ public class WechatShareManager { private static final int THUMB_SIZE = 150; public static final int WECHAT_SHARE_WAY_TEXT = 1; //文字 public static final int WECHAT_SHARE_WAY_PICTURE = 2; //图片 public static final int WECHAT_SHARE_WAY_WEBPAGE = 3; //链接 public static final int WECHAT_SHARE_WAY_VIDEO = 4; //视频 public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession; //会话 public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈 private static WechatShareManager mInstance; private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo; private IWXAPI mWXApi; private Context mContext; private WechatShareManager(Context context){ this.mContext = context; //初始化数据 //初始化微信分享代码 initWechatShare(context); } /** * 获取WeixinShareManager实例 * 非线程安全,请在UI线程中操作 * @return */ public static WechatShareManager getInstance(Context context){ if(mInstance == null){ mInstance = new WechatShareManager(context); } return mInstance; } private void initWechatShare(Context context){ if (mWXApi == null) { mWXApi = WXAPIFactory.createWXAPI(context, WechatShareUtil.WECHAT_APP_ID, true); } mWXApi.registerApp(WechatShareUtil.WECHAT_APP_ID); } /** * 通过微信分享 * @param shareWay 分享的方式(文本、图片、链接) * @param shareType 分享的类型(朋友圈,会话) */ public void shareByWebchat(ShareContent shareContent, int shareType){ switch (shareContent.getShareWay()) { case WECHAT_SHARE_WAY_TEXT: shareText(shareContent, shareType); break; case WECHAT_SHARE_WAY_PICTURE: sharePicture(shareContent, shareType); break; case WECHAT_SHARE_WAY_WEBPAGE: shareWebPage(shareContent, shareType); break; case WECHAT_SHARE_WAY_VIDEO: shareVideo(shareContent, shareType); break; } } private abstract class ShareContent { protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract int getPictureResource(); } /** * 设置分享文字的内容 * @author chengcj1 * */ public class ShareContentText extends ShareContent { private String content; /** * 构造分享文字类 * @param text 分享的文字内容 */ public ShareContentText(String content){ this.content = content; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_TEXT; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } @Override protected int getPictureResource() { return -1; } } /* * 获取文本分享对象 */ public ShareContent getShareContentText(String content) { if (mShareContentText == null) { mShareContentText = new ShareContentText(content); } return (ShareContentText) mShareContentText; } /** * 设置分享图片的内容 * @author chengcj1 * */ public class ShareContentPicture extends ShareContent { private int pictureResource; public ShareContentPicture(int pictureResource){ this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_PICTURE; } @Override protected int getPictureResource() { return pictureResource; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } } /* * 获取图片分享对象 */ public ShareContent getShareContentPicture(int pictureResource) { if (mShareContentPicture == null) { mShareContentPicture = new ShareContentPicture(pictureResource); } return (ShareContentPicture) mShareContentPicture; } /** * 设置分享链接的内容 * @author chengcj1 * */ public class ShareContentWebpage extends ShareContent { private String title; private String content; private String url; private int pictureResource; public ShareContentWebpage(String title, String content, String url, int pictureResource){ this.title = title; this.content = content; this.url = url; this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_WEBPAGE; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return title; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return pictureResource; } } /* * 获取网页分享对象 */ public ShareContent getShareContentWebpag(String title, String content, String url, int pictureResource) { if (mShareContentWebpag == null) { mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureResource); } return (ShareContentWebpage) mShareContentWebpag; } /** * 设置分享视频的内容 * @author chengcj1 * */ public class ShareContentVideo extends ShareContent { private String url; public ShareContentVideo(String url) { this.url = url; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_VIDEO; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return -1; } } /* * 获取视频分享内容 */ public ShareContent getShareContentVideo(String url) { if (mShareContentVideo == null) { mShareContentVideo = new ShareContentVideo(url); } return (ShareContentVideo) mShareContentVideo; } /* * 分享文字 */ private void shareText(ShareContent shareContent, int shareType) { String text = shareContent.getContent(); //初始化一个WXTextObject对象 WXTextObject textObj = new WXTextObject(); textObj.text = text; //用WXTextObject对象初始化一个WXMediaMessage对象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; //构造一个Req SendMessageToWX.Req req = new SendMessageToWX.Req(); //transaction字段用于唯一标识一个请求 req.transaction = buildTransaction("textshare"); req.message = msg; //发送的目标场景, 可以选择发送到会话 WXSceneSession 或者朋友圈 WXSceneTimeline。 默认发送到会话。 req.scene = shareType; mWXApi.sendReq(req); } /* * 分享图片 */ private void sharePicture(ShareContent shareContent, int shareType) { Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); WXImageObject imgObj = new WXImageObject(bitmap); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true); bitmap.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); //设置缩略图 SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } /* * 分享链接 */ private void shareWebPage(ShareContent shareContent, int shareType) { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); if(thumb == null) { Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } /* * 分享视频 */ private void shareVideo(ShareContent shareContent, int shareType) { WXVideoObject video = new WXVideoObject(); video.videoUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(video); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.send_music_thumb); // BitmapFactory.decodeStream(new URL(video.videoUrl).openStream()); /** * 测试过程中会出现这种情况,会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制,所以有可能是大小超过了默认的范围。 * 一般情况下缩略图超出比较常见。Title、description都是文本,一般不会超过。 */ Bitmap thumbBitmap = Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true); thumb.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } }MainActivity.java
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
解决Android调用系统分享图片给微信,出现分享失败,分享多文件必须为图片格式近期应公司需求,分享多图片到微信的功能,之前一直是用微信自己家SDK实现分享,但
最近开发微信公众号内嵌H5页面,使用vue搭建的项目,由于业务需求,需要实现微信自定义分享功能,所以项目中集成微信JS-SDK。微信JS-SDK是微信公众平台面
实现分享功能的几个办法1.调用系统的分享功能2.通过第三方SDK,如ShareSDK,友盟等3.自行使用各自平台的SDK,比如QQ,微信,微博各自的SDK这里就
本文实例讲述了Android编程之微信SDK分享功能过程步骤详细分析。分享给大家供大家参考,具体如下:之前已经分析过怎么用官方的demo分享微信信息了,在这里我
本文实例讲述了Android集成新浪微博第三方登录的方法。分享给大家供大家参考。具体实现方法如下:1.下载微博的sdk,导入微博的jar包两个android-s