1. 程式人生 > >android 微信登入與分享整合

android 微信登入與分享整合

android微信登入與分享這兩個功能是很常用的,尤其是現在微信使用者日益劇增,qq雖然很經典但是感覺大多數用來辦公每個寫android的小夥伴估計都會寫到這個功能,我也是很久沒有寫android了,這相當於是在畢業之後工作上面第一次寫安卓
程式,不管會不會,就是寫。總會有辦法解決問題的。
二、準備工作
1、目前移動應用上微信登入只提供原生的登入方式,需要使用者安裝微信客戶端才能配合使用。

2、對於Android應用,建議總是顯示微信登入按鈕,當用戶手機沒有安裝微信客戶端時,請引導使用者下載安裝微信客戶端。

3、對於iOS應用,考慮到iOS應用商店稽核指南中的相關規定,建議開發者接入微信登入時,先檢測使用者手機是否已安裝微信客戶端(使用sdk中isWXAppInstalled函式 ),對未安裝的使用者隱藏微信登入按鈕,只提供其他登入方式(比如手機號註冊登入、遊客登入等)。
三、授權流程說明
1. 第三方發起微信授權登入請求,微信使用者允許授權第三方應用後,微信會拉起應用或重定向到第三方網站,並且帶上授權臨時票據code引數;

2. 通過code引數加上AppID和AppSecret等,通過API換取access_token;

3. 通過access_token進行介面呼叫,獲取使用者基本資料資源或幫助使用者實現基本操作。
四、整合流程和程式碼,感覺看了上面和官網的的流程也沒有什麼感覺,下面直接開始走流程

(1)微信開放平臺上賬號中心申請成為開發者

(2)在管理中心,建立一個應用,官網填寫一個域名,需要注意的就是應用簽名,應用簽名也不需要此時就填寫,或者隨便填寫一個,後面再用android studio打包生成apk安裝在桌面然後在用工具GenSignature獲取,雖然在程式裡面不用,但是在微信裡面是對應用簽名作了校驗,如果不對會在微信登入時候回撥返回-6,應用包名需要與程式裡面的包名一樣,也就是與清單檔案第二行的包名package一致,應用下載地址可不填

(3)android studio作為開發工具,程式碼目錄結構如圖所示,JavaScriptUtil類多畫進去了。

(4)build.gradle
// 微信sdk
    compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
//google Gson
   compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
(5)AnfroidMainfest.xml
<!--微信登入-->
        <activity android:name=".wxapi.WXEntryActivity"
            android:exported="true">
        </activity>
(6)WXEntryActivity類,在com.iwiteks.PalamarToutism包下面新建wxapi包並新建WXEntryActivity類,注意名字和包名必須是wxapi和WXEntryActivity。
package com.iwiteks.PalmarTourism.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.gson.Gson;
import com.iwiteks.PalmarTourism.bean.WeChatInfo;
import com.iwiteks.PalmarTourism.common.Config;
import com.iwiteks.PalmarTourism.util.JavaScriptUtils;
import com.iwiteks.PalmarTourism.util.OkHttpUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * 創建於 2018/1/30 15:45
 *
 * @author zhaoyi
 *  
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    public  int WX_LOGIN = 1;

    private IWXAPI iwxapi;

    private SendAuth.Resp resp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getSupportActionBar().hide();
        // 隱藏狀態列
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        iwxapi = WXAPIFactory.createWXAPI(this, Config.APP_ID, true);
        //接收到分享以及登入的intent傳遞handleIntent方法,處理結果
        iwxapi.handleIntent(getIntent(), this);

    }


    @Override
    public void onReq(BaseReq baseReq) {
    }



    //請求回撥結果處理
    @Override
    public void onResp(BaseResp baseResp) {
        System.out.println("------------------------" + baseResp.getType());
        //微信登入為getType為1,分享為0
        if (baseResp.getType() == WX_LOGIN){
            //登入回撥
//            System.out.println("------------登陸回撥------------");
            resp = (SendAuth.Resp) baseResp;
//            System.out.println("------------登陸回撥的結果------------:" +  new Gson().toJson(resp));


            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    String code = String.valueOf(resp.code);
                    //獲取使用者資訊
                    getAccessToken(code);
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED://使用者拒絕授權
                    Toast.makeText(WXEntryActivity.this, "使用者拒絕授權", Toast.LENGTH_LONG).show();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://使用者取消
                    Toast.makeText(WXEntryActivity.this, "使用者取消登入", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }else{
            //分享成功回撥
            System.out.println("------------分享回撥------------");
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    //分享成功
                    Toast.makeText(WXEntryActivity.this, "分享成功", Toast.LENGTH_LONG).show();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    //分享取消
                    Toast.makeText(WXEntryActivity.this, "分享取消", Toast.LENGTH_LONG).show();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    //分享拒絕
                    Toast.makeText(WXEntryActivity.this, "分享拒絕", Toast.LENGTH_LONG).show();
                    break;
            }
        }
        finish();
    }

    private void getAccessToken(String code) {
        //獲取授權
        String http = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Config.APP_ID + "&secret=" + Config.APP_SERECET + "&code=" + code + "&grant_type=authorization_code";
        OkHttpUtils.ResultCallback<String> resultCallback = new OkHttpUtils.ResultCallback<String>() {
            @Override
            public void onSuccess(String response) {
                String access = null;
                String openId = null;
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    access = jsonObject.getString("access_token");
                    openId = jsonObject.getString("openid");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //獲取個人資訊
                String getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access + "&openid=" + openId + "";
                OkHttpUtils.ResultCallback<WeChatInfo> resultCallback = new OkHttpUtils.ResultCallback<WeChatInfo>() {
                    @Override
                    public void onSuccess(WeChatInfo response) {
                        response.setErrCode(resp.errCode);
                        Log.i("TAG獲取個人資訊",new Gson().toJson(response));
//                        Toast.makeText(WXEntryActivity.this, new Gson().toJson(response), Toast.LENGTH_LONG).show();
                        finish();
                    }

                    @Override
                    public void onFailure(Exception e) {
                        Toast.makeText(WXEntryActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
                    }
                };
                OkHttpUtils.get(getUserInfo, resultCallback);
            }

            @Override
            public void onFailure(Exception e) {
                Toast.makeText(WXEntryActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
            }
        };
        OkHttpUtils.get(http, resultCallback);
    }
}
(7)WxShareAndLoginUtils類
package com.iwiteks.PalmarTourism.util;

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

import com.iwiteks.PalmarTourism.common.Config;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


/**
 * Created by zhaoyi on 2018/02/01.
 */

public class WxShareAndLoginUtils {

    public static int WECHAT_FRIEND = 0;  //分享好友
    public static int WECHAT_MOMENT = 1;  //分享朋友圈
    private static IWXAPI iwxapi;

    public static IWXAPI getWXAPI(Context context){
        if (iwxapi == null){
            //通過WXAPIFactory建立IWAPI例項
            iwxapi = WXAPIFactory.createWXAPI(context, Config.APP_ID, false);
            //將應用的appid註冊到微信
            iwxapi.registerApp(Config.APP_ID);
        }
        return iwxapi;
    }

    /**
     * 微信登入
     */
    public static void WxLogin(Context context) {
        if (!judgeCanGo(context)){
            return;
        }
        SendAuth.Req req = new SendAuth.Req();
        //授權域 獲取使用者個人資訊則填寫snsapi_userinfo
        req.scope = "snsapi_userinfo";
        //用於保持請求和回撥的狀態 可以任意填寫
        req.state = "test_login";
        iwxapi.sendReq(req);
    }

    /**
     * 分享文字至朋友圈
     * @param text  文字內容
     * @param judge 型別選擇 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxTextShare(Context context,String text,int judge){
        if (!judgeCanGo(context)){
            return;
        }
        //初始化WXTextObject物件,填寫對應分享的文字內容
        WXTextObject textObject = new WXTextObject();
        textObject.text = text;
        //初始化WXMediaMessage訊息物件,
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = textObject;
        message.description = text;
        //構建一個Req請求物件
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());   //transaction用於標識請求
        req.message = message;
        req.scene = judge;      //分享型別 好友==0 朋友圈==1
        //傳送請求
        iwxapi.sendReq(req);
    }

    /**
     *  分享圖片
     * @param bitmap 圖片bitmap,建議別超過32k
     * @param judge 型別選擇 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxBitmapShare(Context context, Bitmap bitmap,int judge){
        if (!judgeCanGo(context)){
            return;
        }
        WXImageObject wxImageObject = new WXImageObject(bitmap);
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = wxImageObject;

        Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap,50,50,true);
        bitmap.recycle();
        message.thumbData = ImageUtil.bmpToByteArray(thunmpBmp,true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = message;
        req.scene = judge;

        iwxapi.sendReq(req);

    }

    /**
     * 網頁分享
     * @param url  地址
     * @param title 標題
     * @param description  描述
     * @param judge  型別選擇 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
     */
    public static void WxUrlShare(Context context,String url,String title,String description,String imgUrl,int judge){
        if (!judgeCanGo(context)){
            return;
        }
        Bitmap bitmap = getBitMBitmap(imgUrl);
        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        wxWebpageObject.webpageUrl = url;

        WXMediaMessage wxMediaMessage = new WXMediaMessage(wxWebpageObject);
        wxMediaMessage.title = title;
        wxMediaMessage.description = description;
        Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap,50,50,true);
        bitmap.recycle();
        wxMediaMessage.thumbData = ImageUtil.bmpToByteArray(thunmpBmp,true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = wxMediaMessage;
        req.scene = judge;

        iwxapi.sendReq(req);
    }


    private static boolean judgeCanGo(Context context){
        getWXAPI(context);
        if (!iwxapi.isWXAppInstalled()) {
            Toast.makeText(context, "請先安裝微信應用", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!iwxapi.isWXAppSupportAPI()) {
            Toast.makeText(context, "請先更新微信應用", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


    /**
     * 圖片url轉bitmap
     */
    public static Bitmap getBitMBitmap(String urlpath) {
        Bitmap map = null;
        try {
            URL url = new URL(urlpath);
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream in;
            in = conn.getInputStream();
            map = BitmapFactory.decodeStream(in);
            // TODO Auto-generated catch block
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
}

(8)OkHttpUtils類
package com.iwiteks.PalmarTourism.util;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * By zhaoyi
 * Description : OkHttp網路連線封裝工具類  解析用的是Gson 記得新增Gson依賴 或者jar包
 */
public class OkHttpUtils {

    private static final String TAG = "OkHttpUtils";

    private static OkHttpUtils mInstance;
    private OkHttpClient mOkHttpClient;
    private Handler mDelivery;
    private Gson mGson;

    private OkHttpUtils() {
        mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        mGson = new Gson();
        mDelivery = new Handler(Looper.getMainLooper());
    }

    private synchronized static OkHttpUtils getmInstance() {
        if (mInstance == null) {
            mInstance = new OkHttpUtils();
        }
        return mInstance;
    }

    private void getRequest(String url, final ResultCallback callback) {
        final Request request = new Request.Builder().url(url).build();
        deliveryResult(callback, request);
    }

    private void postRequest(String url, final ResultCallback callback, List<Param> params) {
        Request request = buildPostRequest(url, params);
        deliveryResult(callback, request);
    }

    /**
     * 處理結果
     * @param callback
     * @param request
     */
    private void deliveryResult(final ResultCallback callback, Request request) {

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                sendFailCallback(callback, e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String str = response.body().string();
                    if (callback.mType == String.class) {
                        /**
                         * 返回字串
                         */
                        sendSuccessCallBack(callback, str);
                    } else {
                        /**
                         * 這裡處理解析返回物件
                         */
                        Object object = mGson.fromJson(str, callback.mType);
                        sendSuccessCallBack(callback, object);

                    }
                } catch (final Exception e) {
//                    LogUtils.e(TAG, "convert json failure", e);
                    sendFailCallback(callback, e);
                }
            }
        });
    }

    private void sendFailCallback(final ResultCallback callback, final Exception e) {
        mDelivery.post(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onFailure(e);
                }
            }
        });
    }

    private void sendSuccessCallBack(final ResultCallback callback, final Object obj) {
        mDelivery.post(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onSuccess(obj);
                }
            }
        });
    }

    private Request buildPostRequest(String url, List<Param> params) {
        FormBody.Builder builder= new FormBody.Builder();
        for (Param param : params) {
            builder.add(param.key, param.value);
        }
        RequestBody requestBody = builder.build();
        return new Request.Builder().url(url).post(requestBody).build();
    }


    /**********************對外介面************************/

    /**
     * get請求
     * @param url  請求url
     * @param callback  請求回撥
     */
    public static void get(String url, ResultCallback callback) {
        getmInstance().getRequest(url, callback);
    }

    /**
     * post請求
     * @param url       請求url
     * @param callback  請求回撥
     * @param params    請求引數
     */
    public static void post(String url, final ResultCallback callback, List<Param> params) {
        getmInstance().postRequest(url, callback, params);
    }

    /**
     * http請求回撥類,回撥方法在UI執行緒中執行
     * @param <T>
     */
    public static abstract class ResultCallback<T> {

        Type mType;

        public ResultCallback(){
            mType = getSuperclassTypeParameter(getClass());
        }

        static Type getSuperclassTypeParameter(Class<?> subclass) {
            Type superclass = subclass.getGenericSuperclass();
            if (superclass instanceof Class) {
                throw new RuntimeException("Missing type parameter.");
            }
            ParameterizedType parameterized = (ParameterizedType) superclass;
            return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
        }

        /**
         * 請求成功回撥
         * @param response
         */
        public abstract void onSuccess(T response);

        /**
         * 請求失敗回撥
         * @param e
         */
        public abstract void onFailure(Exception e);
    }

    /**
     * post請求引數類   這裡可以根據專案抽取成泛型
     */
    public static class Param {

        String key;
        String value;

        public Param() {
        }

        public Param(String key, String value) {
            this.key = key;
            this.value = value;
        }

    }


}
(9)ImageUtil類
package com.iwiteks.PalmarTourism.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

import junit.framework.Assert;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ImageUtil {
	
	private static final String TAG = "SDK_Sample.ImageUtil";
	
	public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		bmp.compress(CompressFormat.PNG, 100, output);
		if (needRecycle) {
			bmp.recycle();
		}
		
		byte[] result = output.toByteArray();
		try {
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static byte[] getHtmlByteArray(final String url) {
		 URL htmlUrl = null;     
		 InputStream inStream = null;     
		 try {         
			 htmlUrl = new URL(url);         
			 URLConnection connection = htmlUrl.openConnection();         
			 HttpURLConnection httpConnection = (HttpURLConnection)connection;         
			 int responseCode = httpConnection.getResponseCode();         
			 if(responseCode == HttpURLConnection.HTTP_OK){             
				 inStream = httpConnection.getInputStream();         
			  }     
			 } catch (MalformedURLException e) {               
				 e.printStackTrace();     
			 } catch (IOException e) {              
				e.printStackTrace();    
		  } 
		byte[] data = inputStreamToByte(inStream);

		return data;
	}
	
	public static byte[] inputStreamToByte(InputStream is) {
		try{
			ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				bytestream.write(ch);
			}
			byte imgdata[] = bytestream.toByteArray();
			bytestream.close();
			return imgdata;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static byte[] readFromFile(String fileName, int offset, int len) {
		if (fileName == null) {
			return null;
		}

		File file = new File(fileName);
		if (!file.exists()) {
			Log.i(TAG, "readFromFile: file not found");
			return null;
		}

		if (len == -1) {
			len = (int) file.length();
		}

		Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

		if(offset <0){
			Log.e(TAG, "readFromFile invalid offset:" + offset);
			return null;
		}
		if(len <=0 ){
			Log.e(TAG, "readFromFile invalid len:" + len);
			return null;
		}
		if(offset + len > (int) file.length()){
			Log.e(TAG, "readFromFile invalid file len:" + file.length());
			return null;
		}

		byte[] b = null;
		try {
			RandomAccessFile in = new RandomAccessFile(fileName, "r");
			b = new byte[len];
			in.seek(offset);
			in.readFully(b);
			in.close();

		} catch (Exception e) {
			Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
			e.printStackTrace();
		}
		return b;
	}
	
	private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440;
	public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) {
		Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);

		BitmapFactory.Options options = new BitmapFactory.Options();

		try {
			options.inJustDecodeBounds = true;
			Bitmap tmp = BitmapFactory.decodeFile(path, options);
			if (tmp != null) {
				tmp.recycle();
				tmp = null;
			}

			Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
			final double beY = options.outHeight * 1.0 / height;
			final double beX = options.outWidth * 1.0 / width;
			Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
			options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
			if (options.inSampleSize <= 1) {
				options.inSampleSize = 1;
			}

			// NOTE: out of memory error
			while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
				options.inSampleSize++;
			}

			int newHeight = height;
			int newWidth = width;
			if (crop) {
				if (beY > beX) {
					newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
				} else {
					newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
				}
			} else {
				if (beY < beX) {
					newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
				} else {
					newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
				}
			}

			options.inJustDecodeBounds = false;

			Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize);
			Bitmap bm = BitmapFactory.decodeFile(path, options);
			if (bm == null) {
				Log.e(TAG, "bitmap decode failed");
				return null;
			}

			Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
			final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
			if (scale != null) {
				bm.recycle();
				bm = scale;
			}

			if (crop) {
				final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height);
				if (cropped == null) {
					return bm;
				}

				bm.recycle();
				bm = cropped;
				Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
			}
			return bm;

		} catch (final OutOfMemoryError e) {
			Log.e(TAG, "decode bitmap failed: " + e.getMessage());
			options = null;
		}

		return null;
	}
}
(9)Config類

package com.iwiteks.PalmarTourism.common;


public class Config {
    public static final String APP_ID = "wx26220645ffbf32";
    //設定估計有問題
    public static final String APP_SERECET = "3086b32df76fe49a1e2863c18799";
}
(10)WechatInfo類
package com.iwiteks.PalmarTourism.bean;

/**
 * Created by zhaoyi on 2016/11/28.
 */

public class WeChatInfo {
    private int errCode;

    private String openid;

    private int sex;

    private String nickname;

    private String headimgurl;

    private String province;

    private String language;

    private String country;

    private String unionid;

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getHeadimgurl() {
        return headimgurl;
    }

    public void setHeadimgurl(String headimgurl) {
        this.headimgurl = headimgurl;
    }

    public String getSex() {
        return (sex == 0) ? "男" : "女";
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    @Override
    public String toString() {
        return "WeChatInfo{" +
                "errCode='" + errCode + '\'' +
                ", openid='" + openid + '\'' +
                ", sex=" + sex +
                ", nickname='" + nickname + '\'' +
                ", headimgurl='" + headimgurl + '\'' +
                ", province='" + province + '\'' +
                ", language='" + language + '\'' +
                ", country='" + country + '\'' +
                ", unionid='" + unionid + '\'' +
                '}';
    }
}

(11)WeiXinActivity類
package com.iwiteks.PalmarTourism.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.iwiteks.PalmarTourism.R;
import com.iwiteks.PalmarTourism.util.WxShareAndLoginUtils;

/**
 * Created by zhaoyi on 2018/2/1.
 */

public class WeiXinActivity extends AppCompatActivity{


    private Button btn,btn_text,btn_image,btn_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weixin);
        btn = (Button) findViewById(R.id.btn);
        btn_text = (Button) findViewById(R.id.btn_text);
        btn_image = (Button) findViewById(R.id.btn_image);
        btn_url = (Button) findViewById(R.id.btn_url);
//        測試微信登入
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WxShareAndLoginUtils.WxLogin(WeiXinActivity.this);
            }
        });
        btn_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WxShareAndLoginUtils.WxTextShare(WeiXinActivity.this,"微信分享",WxShareAndLoginUtils.WECHAT_MOMENT);
            }
        });
        btn_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                WxShareAndLoginUtils.WxBitmapShare(WeiXinActivity.this,bitmap,WxShareAndLoginUtils.WECHAT_MOMENT);
            }
        });
        btn_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           WxShareAndLoginUtils.WxUrlShare(WeiXinActivity.this,"http://www.baidu.com", "百度", "百度一下",
                   "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E7%99%BE%E5%BA%A6logo&step_word=&hs=0&pn=7&spn=0&di=87711457570&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=3295332534%2C276154593&os=1276437059%2C1808669272&simid=3478872735%2C168961746&adpicid=0&lpn=0&ln=1958&fr=&fmq=1517922230276_R&fm=detail&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fwww.swhaifeng.com%2Ffile%2Fupload%2F201406%2F20%2F12-13-15-92-1.png&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bfoiwtujg2_z%26e3Bv54AzdH3Fk6wg1AzdH3Ffi5o-ldl_z%26e3Bip4s&gsm=0&rpstart=0&rpnum=0",
                   WxShareAndLoginUtils.WECHAT_FRIEND);
            }
        });
    }
}
(12)activity_weixin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登入" />
    <Button
        android:id="@+id/btn_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="分享文字" />
    <Button
        android:id="@+id/btn_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="分享圖片" />
    <Button
        android:id="@+id/btn_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="分享網址" />
</LinearLayout>
(13) 到此微信登入與微信與微信分享就可以實現了,若有問題可以私信我