示例#1
0
  public static void shareWechatUrl(
      Context context, boolean isTimeLine, String url, String title, String dsc, Bitmap thumb) {
    IWXAPI api = getWechatApi(context);
    api.registerApp(Constants.APP_ID);

    //        WXTextObject textObj = new WXTextObject();
    //        textObj.text = text;

    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = url;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = dsc;
    if (thumb != null) {
      msg.thumbData = Util.bmpToByteArray(thumb, true);
    }
    //        msg.mediaObject = textObj;

    SendMessageToWX.Req req = new SendMessageToWX.Req();
    req.transaction = buildTransaction("webpage");
    req.message = msg;
    req.scene =
        isTimeLine ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
    api.sendReq(req);
  }
  private void wechatShare(int flag) {
    WXWebpageObject webpage = new WXWebpageObject();
    webpage.webpageUrl = url;
    WXMediaMessage msg = new WXMediaMessage(webpage);
    msg.title = title;
    msg.description = "";
    // 这里替换一张自己工程里的图片资源
    // Bitmap thumb = BitmapFactory.decodeResource(getResources(),
    // R.drawable.action_btn);
    // msg.setThumbImage(thumb);

    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);
  }
 /** 分享朋友圈 */
 private void shareToFriendQuan(
     Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
   Bitmap b = null;
   ByteArrayOutputStream output = null;
   try {
     IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
     api.registerApp(APP_ID);
     WXMediaMessage msg = null;
     if (!api.isWXAppInstalled()) {
       Toast.makeText(
               act,
               act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
               Toast.LENGTH_SHORT)
           .show();
       return;
     }
     switch (type) {
       case Share_Text_Type:
         WXTextObject textObj = new WXTextObject();
         textObj.text = title;
         msg = new WXMediaMessage();
         msg.mediaObject = textObj;
         msg.description = title;
         break;
       case Share_Webpage_Type:
         WXWebpageObject webpage = new WXWebpageObject();
         webpage.webpageUrl = shareUrl;
         msg = new WXMediaMessage(webpage);
         msg.title = title;
         msg.description = des;
         break;
     }
     if (type == Share_Webpage_Type && mDrawable != null) {
       BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
       b = bitmapDrawable.getBitmap();
       if (b == null) {
         bitmapDrawable =
             ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
         b = bitmapDrawable.getBitmap();
       }
       output = new ByteArrayOutputStream();
       b = zoomImg(b, 100);
       b.compress(CompressFormat.PNG, 70, output);
       byte[] result = output.toByteArray();
       msg.thumbData = result;
     }
     SendMessageToWX.Req req = new SendMessageToWX.Req();
     if (type == Share_Text_Type) req.transaction = buildTransaction("text");
     else if (type == Share_Webpage_Type) req.transaction = buildTransaction("webpage");
     req.message = msg;
     req.scene = SendMessageToWX.Req.WXSceneTimeline;
     api.sendReq(req);
   } catch (Exception e) {
     Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
   } finally {
     if (b != null) {
       b = null;
     }
     if (output != null) {
       try {
         output.close();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
  private void shareToWeixin(
      Context mcontext, String shareUrl, String title, String des, Drawable mDrawable, int type) {
    Bitmap b = null;
    try {
      IWXAPI api = WXAPIFactory.createWXAPI(act, APP_ID);
      api.registerApp(APP_ID);
      if (!api.isWXAppInstalled()) {
        Toast.makeText(
                act,
                act.getResources().getString(R.string.weixinsdk_demo_has_not_installed_weibo),
                Toast.LENGTH_SHORT)
            .show();
        return;
      }
      WXMediaMessage msg = null;
      switch (type) {
        case Share_Text_Type:
          WXTextObject textObj = new WXTextObject();
          textObj.text = title;
          msg = new WXMediaMessage();
          msg.mediaObject = textObj;
          msg.description = title;
          break;
        case Share_Webpage_Type:
          WXWebpageObject webpage = new WXWebpageObject();
          webpage.webpageUrl = shareUrl;
          msg = new WXMediaMessage(webpage);
          msg.title = title;
          msg.description = des;
          break;
      }

      if (type == Share_Webpage_Type && mDrawable != null) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
        b = bitmapDrawable.getBitmap();
        b = zoomImg(b, 100);
        if (b == null) {
          bitmapDrawable =
              ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher));
          b = bitmapDrawable.getBitmap();
        }
        msg.thumbData = bmpToByteArray(b, true);
      }
      SendMessageToWX.Req req = new SendMessageToWX.Req();
      if (type == Share_Text_Type) req.transaction = buildTransaction("text");
      else if (type == Share_Webpage_Type) {
        req.transaction = buildTransaction("webpage");
        msg.title = title;
        msg.description = des;
      }
      req.message = msg;
      req.scene = SendMessageToWX.Req.WXSceneSession;
      api.sendReq(req);
    } catch (Exception e) {
      e.printStackTrace();
      Toast.makeText(mcontext, mcontext.getString(R.string.share_weixin), 300).show();
    } finally {
      if (b != null) {
        b = null;
      }
    }
  }