一. 配置
(1)module下build.gradle
implementation ('com.xyzlf.share:sharesdk:0.0.10') { exclude group: 'com.android.support', module: 'appcompat-v7' }
(2)AndroidManifest.xml
权限:
与activity平级,设置微信key:
二. 实现
(1)注册wxapi,
IWXAPI wxapi = WXAPIFactory.createWXAPI(this, "你的微信appid", true); // 注册 wxapi.registerApp("你的微信appid");
(2)分享图片方法
private static final int THUMB_SIZE = 150;
/** * 分享图片 * @param bitmap 图片 * @param shareType 0:分享到好友 1:分享到朋友圈 */ private void sharePicture(Bitmap bitmap, int shareType) { 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); //设置缩略图 SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; wxapi.sendReq(req); }
public static byte[] bmpToByteArray(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); }
(3)截屏方法(截取当前界面)
/** * 截屏 * * @param activity 需要截屏的activity * @return */ public static Bitmap capture(Activity activity) { activity.getWindow().getDecorView().setDrawingCacheEnabled(true); Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache(); return bmp; }
(4)调用
sharePicture(capture(MainActivity.this), 0);
至此,分享图片功能完成。
三. 扩展
下面是微信分享文字,链接,视频的
/* * 分享文字 */ private void shareText(String text, int shareType) { //初始化一个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 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); } 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.ic_launcher_background); // 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); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }