最近项目中需要微信分享给好友功能。
这里做一下小结:
1、使用微信提供的sdk做分享
分享带图片的链接给好友或朋友圈
* @param flag * (0:分享到微信好友,1:分享到微信朋友圈)
private void wechatShare(int flag,Bitmap bitmap,String url ,String describe) { Log.d("MyBarCodeActivity", "---------------执行微信委托 链接url是 "+url); if (!wxApi.isWXAppInstalled()) { Toast.makeText(this, "您还未安装微信客户端", Toast.LENGTH_SHORT).show(); return; } WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = url; WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = "接送委托"; msg.description = describe; msg.thumbData = Util.bmpToByteArray(bitmap, false,100); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = String.valueOf(System.currentTimeMillis()); req.message = msg; req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline; wxApi.sendReq(req); }(微信sdk直接分享图片的功能就不介绍了,微信官方文档及demo中都有,比较简单)
2、使用ShareSDK
/** * 使用shareSDK做的微信好友分享(跟直接微信sdk分享的效果一样) * @param bitmap * @param url * @param describe */ private void wechatShare(Bitmap bitmap,String url ,String describe){ if (!wxApi.isWXAppInstalled()) { Toast.makeText(this, "您还未安装微信客户端", Toast.LENGTH_SHORT).show(); return; } Wechat.ShareParams sp = new Wechat.ShareParams(); sp.setShareType(Platform.SHARE_WEBPAGE); sp.setTitle("接送委托"); sp.setText(describe); sp.setUrl(url); sp.setImageData(bitmap); Platform wechat = ShareSDK.getPlatform(Wechat.NAME); wechat.share(sp); }
3、使用Intent做微信分享,无需微信认证的key
public void shareToFriend(Context context,String Kdescription) { //分享图文至朋友圈 Intent intent = new Intent(); intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI")); intent.setAction("android.intent.action.SEND"); intent.setType("image/*"); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra("Kdescription", Kdescription); //微信分享页面,图片上边的描述 // Bitmap bt= BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.arrow_up); // final Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bt, null,null)); Uri mUri=Uri.fromFile(saveImage(barCodeBitmap)); Log.d("MyBarCodeActivity", "-------------shareToFriend uri=="+mUri); intent.putExtra(Intent.EXTRA_STREAM, mUri); context.startActivity(intent); } public void shareToFriendsCircle(Context context,String Kdescription) { //分享图文至朋友圈 Intent intent = new Intent(); intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI")); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra("Kdescription", Kdescription); //微信分享页面,图片上边的描述 Uri mUri=Uri.fromFile(saveImage(barCodeBitmap)); intent.putExtra(Intent.EXTRA_STREAM, mUri); context.startActivity(intent); }
遗憾的是各种直接分享图片到微信好友的功能都没有实现带describe的图片分享功能。
也正是因为无法添加描述信息,项目中转而使用了页面链接的分享功能。