当前位置:   article > 正文

Android Studio 微信分享功能(包括可以分享到朋友圈,分享到朋友)_android studio微信sdk分享 设备不安装微信

android studio微信sdk分享 设备不安装微信

第一步:配置sdk
在build.gradle文件中,添加如下依赖即可:
在Android Studio中新建你的工程,并保证网络设置可以成功从jcenter下载微信SDK即可。

dependencies {
    compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
  • 1
  • 2
  • 3

或者是以下

dependencies {
    compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
  • 1
  • 2
  • 3

AndroidManifest.xml 设置

添加必要的权限支持:

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

第二步
WechatShareManager.java 完善

 package com.jackie.umeng.share;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
 
/**
 * 实现微信分享功能的核心类
 * @author chengcj1
 *
 */
public class WechatShareManager {
     
    private static final int THUMB_SIZE = 150;
    
    public static final int WECHAT_SHARE_WAY_TEXT = 1;   //文字
    public static final int WECHAT_SHARE_WAY_PICTURE = 2; //图片
    public static final int WECHAT_SHARE_WAY_WEBPAGE = 3;  //链接
    public static final int WECHAT_SHARE_WAY_VIDEO = 4; //视频
    public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession;  //会话
    public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈
    
    private static WechatShareManager mInstance;
    private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo;
    private IWXAPI mWXApi;
    private Context mContext;
     
    private WechatShareManager(Context context){
        this.mContext = context;
        //初始化数据
        //初始化微信分享代码
        initWechatShare(context);
    }
     
    /**
     * 获取WeixinShareManager实例
     * 非线程安全,请在UI线程中操作
     * @return
     */
   

     public static WechatShareManager getInstance(Context context){
            if(mInstance == null){
                mInstance = new WechatShareManager(context);
            }
            return mInstance;
        }
         
        private void initWechatShare(Context context){
        	if (mWXApi == null) {
        		mWXApi = WXAPIFactory.createWXAPI(context, WechatShareUtil.WECHAT_APP_ID, true);
        	}
            mWXApi.registerApp(WechatShareUtil.WECHAT_APP_ID);
        }
         
        /**
         * 通过微信分享
         * @param shareWay 分享的方式(文本、图片、链接)
         * @param shareType 分享的类型(朋友圈,会话)
         */
        public void shareByWebchat(ShareContent shareContent, int shareType){
            switch (shareContent.getShareWay()) {
            case WECHAT_SHARE_WAY_TEXT:
                shareText(shareContent, shareType);
                break;
            case WECHAT_SHARE_WAY_PICTURE:
                sharePicture(shareContent, shareType);
                break;
            case WECHAT_SHARE_WAY_WEBPAGE:
                shareWebPage(shareContent, shareType);
                break;
            case WECHAT_SHARE_WAY_VIDEO:
            	shareVideo(shareContent, shareType);
            	break;
            }
        }
         
        private abstract class ShareContent {
            protected abstract int getShareWay();
            protected abstract String getContent();
            protected abstract String getTitle();
            protected abstract String getURL();
            protected abstract int getPictureResource();
        }
         
        /**
         * 设置分享文字的内容
         * @author chengcj1
         *
         */
        public class ShareContentText extends ShareContent {
            private String content;
             
            /**
             * 构造分享文字类
             * @param text 分享的文字内容
             */
            public ShareContentText(String content){
                this.content = content;
            }
            
            @Override
            protected int getShareWay() {
                return WECHAT_SHARE_WAY_TEXT;
            }
     
            @Override
            protected String getContent() {
                return content;
            }
     
            @Override
            protected String getTitle() {
                return null;
            }
     
            @Override
            protected String getURL() {
                return null;
            }
     
            @Override
            protected int getPictureResource() {
                return -1;
            }
        }
        
        /*
         * 获取文本分享对象
         */
        public ShareContent getShareContentText(String content) {
        	if (mShareContentText == null) {
        		mShareContentText = new ShareContentText(content);
        	}
        	return (ShareContentText) mShareContentText;
        }
         
        /**
         * 设置分享图片的内容
         * @author chengcj1
         *
         */
        public class ShareContentPicture extends ShareContent {
            private int pictureResource;
            public ShareContentPicture(int pictureResource){
                this.pictureResource = pictureResource;
            }
            
            @Override
            protected int getShareWay() {
                return WECHAT_SHARE_WAY_PICTURE;
            }
            
            @Override
            protected int getPictureResource() {
                return pictureResource;
            }
             
            @Override
            protected String getContent() {
                return null;
            }
     
            @Override
            protected String getTitle() {
                return null;
            }
     
            @Override
            protected String getURL() {
                return null;
            }
        }
        
        /*
         * 获取图片分享对象
         */
        public ShareContent getShareContentPicture(int pictureResource) {
        	if (mShareContentPicture == null) {
        		mShareContentPicture = new ShareContentPicture(pictureResource);
        	}
        	return (ShareContentPicture) mShareContentPicture;
        }
         
        /**
         * 设置分享链接的内容
         * @author chengcj1
         *
         */
        public class ShareContentWebpage extends ShareContent {
            private String title;
            private String content;
            private String url;
            private int pictureResource;
            public ShareContentWebpage(String title, String content, String url, int pictureResource){
                this.title = title;
                this.content = content;
                this.url = url;
                this.pictureResource = pictureResource;
            }
            
            @Override
            protected int getShareWay() {
                return WECHAT_SHARE_WAY_WEBPAGE;
            }
     
            @Override
            protected String getContent() {
                return content;
            }
     
            @Override
            protected String getTitle() {
                return title;
            }
     
            @Override
            protected String getURL() {
                return url;
            }
     
            @Override
            protected int getPictureResource() {
                return pictureResource;
            }
        }
        
        /*
         * 获取网页分享对象
         */
        public ShareContent getShareContentWebpag(String title, String content, String url, int pictureResource) {
        	if (mShareContentWebpag == null) {
        		mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureResource);
        	}
        	return (ShareContentWebpage) mShareContentWebpag;
        }
        
        /**
         * 设置分享视频的内容
         * @author chengcj1
         *
         */
        public class ShareContentVideo extends ShareContent {
        	private String url;
        	public ShareContentVideo(String url) {
        		this.url = url;
        	}
    
    		@Override
    		protected int getShareWay() {
    			return WECHAT_SHARE_WAY_VIDEO;
    		}
    
    		@Override
    		protected String getContent() {
    			return null;
    		}
    
    		@Override
    		protected String getTitle() {
    			return null;
    		}
    
    		@Override
    		protected String getURL() {
    			return url;
    		}
    
    		@Override
    		protected int getPictureResource() {
    			return -1;
    		}
        }
        
        /*
         * 获取视频分享内容
         */
        public ShareContent getShareContentVideo(String url) {
        	if (mShareContentVideo == null) {
        		mShareContentVideo = new ShareContentVideo(url);
        	}
        	return (ShareContentVideo) mShareContentVideo;
        } 
         
        /*
         * 分享文字
         */
        private void shareText(ShareContent shareContent, int shareType) {
            String text = shareContent.getContent();
            //初始化一个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 sharePicture(ShareContent shareContent, int shareType) {
            Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource());
            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, true);  //设置缩略图
             
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("imgshareappdata");
            req.message = msg;
            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, true);
            }
             
            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.send_music_thumb);
    //		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, true);
            
    		SendMessageToWX.Req req = new SendMessageToWX.Req();
    		req.transaction = buildTransaction("video");
    		req.message = msg;
    		req.scene =  shareType;
    		mWXApi.sendReq(req);
        }
         
        private String buildTransaction(final String type) {
            return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393

MainActivity.java

package com.jackie.umeng.share;

import com.jackie.umeng.share.WechatShareManager.ShareContentPicture;
import com.jackie.umeng.share.WechatShareManager.ShareContentText;
import com.jackie.umeng.share.WechatShareManager.ShareContentVideo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button mShareText, mSharePicture, mShareVideo;
	private WechatShareManager mShareManager; 
	
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mShareText = (Button) findViewById(R.id.share_text);
		mSharePicture = (Button) findViewById(R.id.share_picture);
		mShareVideo = (Button) findViewById(R.id.share_video);
		mShareText.setOnClickListener(this);
		mSharePicture.setOnClickListener(this);
		mShareVideo.setOnClickListener(this);
		
		mContext = this;
		
		mShareManager = WechatShareManager.getInstance(mContext);
	}

	@Override
	public void onClick(View v) {
		 if(isWeixinAvilible(mContext)) {
         
        }else {
            Toast.makeText(mContext, "您还没有安装微信,请先安装微信客户端", Toast.LENGTH_SHORT).show();
        }

		switch (v.getId()) {
		case R.id.share_text:
			ShareContentText mShareContentText = (ShareContentText) mShareManager.getShareContentText("微信文本分享");
			mShareManager.shareByWebchat(mShareContentText, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
			break;
		case R.id.share_picture:
			ShareContentPicture mShareContentPicture = (ShareContentPicture) mShareManager.getShareContentPicture(R.drawable.share);
			mShareManager.shareByWebchat(mShareContentPicture, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
		    break;
		case R.id.share_video:
			ShareContentVideo mShareContentVideo = (ShareContentVideo) mShareManager.getShareContentVideo("http://baidu.hz.letv.com/kan/agSlT?fr=v.baidu.com/");
			mShareManager.shareByWebchat(mShareContentVideo, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
			break;
		default:
			break;
		}
	}
	
  /**
 * 判断 用户是否安装微信客户端
 */
public static boolean isWeixinAvilible(Context context) {
    final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
    List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
    if (pinfo != null) {
        for (int i = 0; i < pinfo.size(); i++) {
            String pn = pinfo.get(i).packageName;
            if (pn.equals("com.tencent.mm")) {
                return true;
            }
        }
    }
    return false;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

如果后台返给我们的数据是二进制流图片我们需要做一些改动如下:
二进制流转换成Bitmap类型的

第一步:

 /**
     * base64转为bitmap
     * @return
     */
    public static Bitmap base64ToBitmap(String picStrInMsg) {
        Bitmap bitmap = null;
        byte[] decode = Base64.decode(picStrInMsg, Base64.DEFAULT);

        bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);

        return bitmap;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第二步:
在调用微信分享的方法里面

 Bitmap bitmap = base64ToBitmap(arg.split("base64,")[1]);
            try {

                WXImageObject imgObj = new WXImageObject(bitmap);
                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = imgObj;
                Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
                bitmap.recycle();
                msg.thumbData = Util.bmpToByteArray(thumbBitmap, true);  //设置缩略图
                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("imgshareappdata");
                req.message = msg;
                req.scene = WechatShareManager.WECHAT_SHARE_TYPE_TALK;
                mShareManager.mWXApi.sendReq(req);
            } catch (ActivityNotFoundException e) {
// TODO: handle exception
                Toast.makeText(mContext, "检查到您手机没有安装微信,请安装后使用该功能", Toast.LENGTH_LONG).show();
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

《-----------到这里就结束了以下的以下都是遇见问题的解决办法,希望对自己有帮助对大家有帮助---------》
最后如果出现以下问题isWebchatAvaliable()检查微信不好用请换成以下:

最后如果出现以下问题
在这里插入图片描述
做微信分享功能,遇到这个问题,在网上也没找到答案。最后发现问题解决了,我出现这个问题的原因是: APP_ID 的 错误,APP_ID一般以 wx 开头的18个字节长度的字符串。也就是说你的微信ID不对。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/292924
推荐阅读
相关标签
  

闽ICP备14008679号