記微信分享網頁出現的問題
阿新 • • 發佈:2019-02-15
寫微信分享的時候想著挺簡單的,寫了一半出現的錯誤分享出來。
第一個:
使用BottomDialog實現底部彈框的時候出現:
java.lang.NoClassDefFoundError
本來想著使用這個框架很快就實現了彈框的功能,程式碼就那幾行,加入compile 'top.zibin:BottomDialog:1.0.4'
超級簡單,但是出現這個問題找了兩三個小時到快放棄,一直在網上查詢別人出現問題的解決辦法,無果。。第二天早上來的時候突然看到框架裡面有報錯,原來裡面使用到一個recycle包,然後在build gradle新增compile 'com.android.support:recyclerview-v7:25.0.1'
第二個:
使用微信支付分享網頁的時候點選一直沒有反應,然後試了一下分享文字和圖片都可以分享成功,只有網頁的時候分享不成功。
解決辦法:把分享網頁的縮圖不能超過32K,問題成功解決。
總結經驗:出現問題不要陷到自己認為錯誤的怪圈,可能出來溜達一圈或者吃個飯再回來看問題,一眼就出來了。哈哈,省時省力的辦法。
再分享一個在網上找的封裝微信分享的包,拿來直接用。
WechatShareManager:
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;
public 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, ApiUtils.WX_ID, true);
}
mWXApi.registerApp(ApiUtils.WX_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 = PicUtils.bitmap2Bytes(thumbBitmap); //設定縮圖
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 = PicUtils.bitmap2Bytes(thumb);
}
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.mipmap.ic_launcher);
// 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 = PicUtils.bitmap2Bytes(thumbBitmap);
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();
}
}
在點選分享的按鈕觸發事件
//分享
new BottomDialog(getActivity())
.title("分享到")
.orientation(BottomDialog.HORIZONTAL)
.inflateMenu(R.menu.menu_share)
.itemClick( new OnItemClickListener() {
@Override
public void click(Item item) {
if (!isWebchatAvaliable()) {
Toast.makeText(mContext, "請先安裝微信", Toast.LENGTH_LONG).show();
return;
}
mWechatShareManager= WechatShareManager.getInstance(getActivity()); ;
switch (item.getId()){
case R.id.moments:
//分享到朋友圈
// WechatShareManager.ShareContentText mShareContentText = (WechatShareManager.ShareContentText) mWechatShareManager.getShareContentText("微信文字分享");
WechatShareManager.ShareContentWebpage mSharePage = (WechatShareManager.ShareContentWebpage) mWechatShareManager.getShareContentWebpag(getString(R.string.share_title),shareDesc,ApiUtils.SHARE_URL,R.mipmap.share1);
mWechatShareManager.shareByWebchat(mSharePage, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS );
break;
case R.id.wechat:
// WechatShareManager.ShareContentText mShareContentText1 = (WechatShareManager.ShareContentText) mWechatShareManager.getShareContentText("微信文字分享");
WechatShareManager.ShareContentWebpage mSharePage2 = (WechatShareManager.ShareContentWebpage) mWechatShareManager.getShareContentWebpag(getString(R.string.share_title),shareDesc,ApiUtils.SHARE_URL,R.mipmap.share1);
mWechatShareManager.shareByWebchat(mSharePage2, WechatShareManager.WECHAT_SHARE_TYPE_TALK );
break;
}
}
})
.show();
程式碼中有涉及bitmap轉位元組的方法自行百度,很簡單的。