Android 實現微信分享好友和朋友圈
阿新 • • 發佈:2019-02-13
1.在微信開放平臺 建立移動應用,並通過稽核,這裡要注意的是應用簽名要一致,(下載官網上的APK,輸入包名就可以生成了)
2.具體用法可以看官網的文件,這裡只總結使用過程遇到的問題和解決方法
3.
參照這個用法分享是可以的,但是如果你的圖片大於32kb就不會調微信分享的介面。
MicroMsg.SDK.WXApiImplV10: sendReq checkArgs fail
問題:如何解決大於32kb圖片不調微信的問題
解決方案:1.因為圖片是後臺返回的url,android 端這邊暫時只能將本地的資源壓縮,而不能壓縮url的圖片,會提示說找不到資源。。(目前我用過很多辦法都不行,如果有好的方法希望分享給我,謝謝)。所以目前我這邊是後臺壓縮完圖片返回給我
public class WeChatShareUtil {
//從官網申請的合法appId
private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
//IWXAPI是第三方app和微信通訊的openapi介面
private IWXAPI api;
private Context context;
public static WeChatShareUtil weChatShareUtil;
public static WeChatShareUtil getInstance (Context context) {
if (weChatShareUtil == null) {
weChatShareUtil = new WeChatShareUtil();
}
if (weChatShareUtil.api != null) {
weChatShareUtil.api.unregisterApp();
}
weChatShareUtil.context = context;
weChatShareUtil.regToWx();
return weChatShareUtil;
}
//註冊應用id到微信
private void regToWx() {
//通過WXAPIFactory工廠,獲取IWXAPI的例項
api = WXAPIFactory.createWXAPI(context, Contonts.APP_ID, false);
//將應用的appId註冊到微信
api.registerApp(Contonts.APP_ID);
}
/**
* 分享文字到朋友圈或者好友
*
* @param text 文字內容
* @param scene 分享方式:好友還是朋友圈
*/
public boolean shareText(String text, int scene) {
//初始化一個WXTextObject物件,填寫分享的文字物件
WXTextObject textObj = new WXTextObject();
textObj.text = text;
return share(textObj, text, scene);
}
/**
* 分享圖片到朋友圈或者好友
*
* @param bmp 圖片的Bitmap物件
* @param scene 分享方式:好友還是朋友圈
*/
public boolean sharePic(Bitmap bmp, int scene) {
//初始化一個WXImageObject物件
WXImageObject imageObj = new WXImageObject(bmp);
//設定縮圖
Bitmap thumb = Bitmap.createScaledBitmap(bmp, 60, 60, true);
bmp.recycle();
return share(imageObj, thumb, scene);
}
/**
* 分享網頁到朋友圈或者好友,視訊和音樂的分享和網頁大同小異,只是建立的物件不同。
* 詳情參考官方文件:
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN
*
* @param url 網頁的url
* @param title 顯示分享網頁的標題
* @param description 對網頁的描述
* @param scene 分享方式:好友還是朋友圈
*/
public boolean shareUrl(String url, String title, Bitmap thumb, String description, int scene) {
//初試話一個WXWebpageObject物件,填寫url
WXWebpageObject webPage = new WXWebpageObject();
webPage.webpageUrl = url;
return share(webPage, title, thumb, description, scene);
}
private boolean share(WXMediaMessage.IMediaObject mediaObject, Bitmap thumb, int scene) {
return share(mediaObject, null, thumb, null, scene);
}
private boolean share(WXMediaMessage.IMediaObject mediaObject, String description, int scene) {
return share(mediaObject, null, null, description, scene);
}
private boolean share(WXMediaMessage.IMediaObject mediaObject, String title, Bitmap thumb, String description, int scene) {
//初始化一個WXMediaMessage物件,填寫標題、描述
WXMediaMessage msg = new WXMediaMessage(mediaObject);
if (title != null) {
msg.title = title;
}
if (description != null) {
msg.description = description;
}
if (thumb != null) {
msg.thumbData = bmpToByteArray(thumb, true);
}
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = scene;
return api.sendReq(req);
}
//判斷是否支援轉發到朋友圈
//微信4.2以上支援,如果需要檢查微信版本支援API的情況, 可呼叫IWXAPI的getWXAppSupportAPI方法,0x21020001及以上支援傳送朋友圈
public boolean isSupportWX() {
int wxSdkVersion = api.getWXAppSupportAPI();
return wxSdkVersion >= TIMELINE_SUPPORTED_VERSION;
}
private byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
可以看到shareUrl(String url, String title, Bitmap thumb, String description, int scene)
圖片必須是Bitmap
於是我這邊就要將url轉換為Bitmap
/**
* 根據圖片的url路徑獲得Bitmap物件
*/
public static Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
如果有其他好的實現方式歡迎在評論中留言,我實踐過之後會對應更新 ,謝謝!