zoukankan      html  css  js  c++  java
  • 微信分享封装类

     发现一个微信的bug:IOS版的微信查看分享的图片可以长按弹出识别图片中的二维码提示,Android版没有,这是对Android赤裸裸的歧视啊

    需要注意的一点:微信分享中涉及到图片的,一律都要压缩至120*120,否则会报错

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AlertDialog;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
    
    import com.tencent.mm.sdk.openapi.IWXAPI;
    import com.tencent.mm.sdk.openapi.SendMessageToWX;
    import com.tencent.mm.sdk.openapi.WXAPIFactory;
    import com.tencent.mm.sdk.openapi.WXImageObject;
    import com.tencent.mm.sdk.openapi.WXMediaMessage;
    import com.tencent.mm.sdk.openapi.WXTextObject;
    import com.tencent.mm.sdk.openapi.WXVideoObject;
    import com.tencent.mm.sdk.openapi.WXWebpageObject;
    import com.tencent.mm.sdk.platformtools.Util;
    import com.xuehu365.xuehu.R;
    import com.xuehu365.xuehu.model.Constant;
    import com.xuehu365.xuehu.model.VideoEntity;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    /**
     * Created by Administrator on 2016/9/6.
     */
    public class WXShareUtils {
    
        private static IWXAPI wxApi;
        private static RelativeLayout wx_share_friend;
        private static RelativeLayout wx_share_pengyouquan;
    
        private static Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                SendMessageToWX.Req req = (SendMessageToWX.Req) msg.obj;
    
                //调用api接口发送数据到微信
                wxApi.sendReq(req);
            }
        };
    
        private static void init(Context context) {
            //实例化
            wxApi = WXAPIFactory.createWXAPI(context, Constant.wxAppId);
            wxApi.registerApp(Constant.wxAppId);
            //判断是否有安装微信
            if (!wxApi.isWXAppInstalled()) {
                Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
                return;
            }
        }
    
        //显示微信分享选择页面
        public static void showShareSelect(final Context context, final String title, final String description,
                                           final String icon, final String webUrl, final int contentType) {
            View view = LayoutInflater.from(context).inflate(R.layout.share_window, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogTheme);
            builder.setView(view);
            final AlertDialog shareDialog = builder.create();
            //显示在底部
            Window window = shareDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.dialog_animation);
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            shareDialog.show();
    
            wx_share_friend = (RelativeLayout) view.findViewById(R.id.wx_share_friend);
            wx_share_pengyouquan = (RelativeLayout) view.findViewById(R.id.wx_share_pengyouquan);
    
            //分享给好友
            wx_share_friend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (contentType) {
                        case Constant.wxText:
                            WXShareUtils.shareText(context, "分享", Constant.wxFriend);
                            break;
                        case Constant.wxVideo:
                            WXShareUtils.shareVideo(context, null, Constant.wxFriend);
                            break;
                        case Constant.wxWeb:
                            WXShareUtils.shareWeb(context, title, description, icon, webUrl, Constant.wxFriend);
                            break;
                    }
                    shareDialog.dismiss();
                }
            });
    
            //分享到朋友圈
            wx_share_pengyouquan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (contentType) {
                        case Constant.wxText:
                            WXShareUtils.shareText(context, "分享", Constant.wxQuan);
                            break;
                        case Constant.wxVideo:
                            WXShareUtils.shareVideo(context, null, Constant.wxQuan);
                            break;
                        case Constant.wxWeb:
                            WXShareUtils.shareWeb(context, title, description, icon, webUrl, Constant.wxQuan);
                            break;
                    }
                    shareDialog.dismiss();
                }
            });
        }
    
        /*
        分享文本
         */
        public static void shareText(Context context, String text, String type) {
            init(context);
            //初始化一个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();
            req.transaction = buildTransaction("text");
            req.message = msg;
            if (Constant.wxFriend.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneSession;
            } else if (Constant.wxQuan.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
            }
            //调用api接口发送数据到微信
            wxApi.sendReq(req);
        }
    
    
        public static String buildTransaction(final String type) {
            return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
        }
    
        /**
         * 图片分享
         */
        public static void shareImage(Context context, Bitmap bitmap, String type) {
            init(context);
            WXImageObject imageObj = new WXImageObject(bitmap);
            WXMediaMessage msg = new WXMediaMessage();
            msg.mediaObject = imageObj;
    
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 120, 120, true);
            bitmap.recycle();
            msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
    
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("img");
            req.message = msg;
            if (Constant.wxFriend.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneSession;
            } else if (Constant.wxQuan.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
            }
            wxApi.sendReq(req);
        }
    
        /*
        分享视频
         */
        public static void shareVideo(final Context context, VideoEntity videoEntity, String type) {
            init(context);
            //初始化一个WXVideoObject对象,填写url
            WXVideoObject video = new WXVideoObject();
            video.videoUrl = videoEntity.getVideoVid();
    
            //用一个WXVideoObject对象初始化一个WXMediaMessage对象,填写标题、描述
            final WXMediaMessage msg = new WXMediaMessage(video);
            msg.title = videoEntity.getTitle();
            msg.description = videoEntity.getTitle();
            Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_logo);
            msg.thumbData = Util.bmpToByteArray(thumb, true);
    
            //构造一个Req
            final SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("video");
            req.message = msg;
            if (Constant.wxFriend.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneSession;
            } else if (Constant.wxQuan.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
            }
    
            //调用api接口发送数据到微信
            wxApi.sendReq(req);
        }
    
        /*
        分享网页
         */
        public static void shareWeb(final Context context, String title, String description, final String icon, String webUrl, String type) {
            init(context);
            //初始化一个WXWebpageObject对象,填写URL
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = webUrl;
    
            //用WXWebpageObject对象初始化一个WXMediaMessage对象,填写标题、描述
            final WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = title;
            msg.description = description;
            //构造一个Req
            final SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("webpage");
            if (Constant.wxFriend.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneSession;
            } else if (Constant.wxQuan.equals(type)) {
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
            }
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = loadBitmap(icon);
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 120, 120, true);
                    msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
    
                    req.message = msg;
                    Message message = mHandler.obtainMessage();
                    message.obj = req;
                    mHandler.sendMessage(message);
                }
            }).start();
        }
    
        //下载网络图片
        private static Bitmap loadBitmap(String icon) {
            try {
                URL url = new URL(icon);
                InputStream is = url.openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                return bitmap;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

     

  • 相关阅读:
    九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)
    九度OJ 1153:括号匹配问题 (DP)
    九度OJ 1152:点菜问题 (01背包、DP)
    九度OJ 1151:位操作练习 (位操作)
    数论——素数算法
    wubi安装ubuntu-12.04.3
    快速FQ
    linux下的软硬链接区别
    死锁问题总结
    windows下用XShell远程ubuntu时连接失败
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5846918.html
Copyright © 2011-2022 走看看