android开发之微信分享小计

最近项目中需要微信分享给好友功能。

这里做一下小结:

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的图片分享功能。

也正是因为无法添加描述信息,项目中转而使用了页面链接的分享功能。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值