先看一下效果圖 java
1.產品需求說明:商品分享因此在進入商品詳情頁,請求後端,獲取分享的內容(包括:標題,描述,圖片,連接)android
GoodDetailActivity相關部分代碼以下:web
public class GoodDetailActivity extends BaseActivity<GoodsViewModel, ActivityGoodsDetailBinding> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_detail);
getShareByGoodId();
}
private ShareResult shareResult;
/**
* 分享
*/
private void getShareByGoodId(){
viewModel.getShareByGoodId(goods_id).observe(this, shareResult -> {
if (shareResult != null){
this.shareResult = shareResult;
getImgBitmap();
}
});
}
/**
* 關鍵點!!!
* 網絡圖片url轉Bitmap
*/
private void getImgBitmap(){
Glide.with(this).asBitmap().load(shareResult.getShare_image()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//2
Bitmap thumbBmp = null;
if (resource.getHeight()>100 && resource.getWidth()>100) {
thumbBmp = Bitmap.createScaledBitmap(resource, 100, 100, true);
}else {
thumbBmp = resource;
}
if (thumbBmp != null) {
shareResult.setBitmap(thumbBmp);
// dismissLoading();
// DialogUtils.shareDailog(mContext, url, "標題", "內容", thumbBmp);
} else {
// dismissLoading();
// ToastUtils.showShort("獲取分享圖片失敗");
}
}
});
}
/**
*點擊分享按鈕
*/
public void onClickShare(View view){
if (shareResult == null){
getShareByGoodId();
return;
}
toShare();
}
private void toShare(){
String url = shareResult.getShare_image();
if (shareResult.getShare_link() != null && !"".equals(shareResult.getShare_link()) ){
url = shareResult.getShare_link();
}
WXShareUtil.wxShare(this, url, shareResult.getShare_title(),
shareResult.getShare_description(), false, shareResult.getBitmap());
}
}
複製代碼
分享返回類ShareResult代碼:後端
public class ShareResult {
/**
* "share_title": null,
* "share_description": null,
* "share_image": "http://.....com",
* "share_link": ""
*/
private String share_title;
private String share_description;
private String share_image;
private String share_link;
private Bitmap bitmap;
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public String getShare_title() {
return share_title;
}
public void setShare_title(String share_title) {
this.share_title = share_title;
}
public String getShare_description() {
return share_description;
}
public void setShare_description(String share_description) {
this.share_description = share_description;
}
public String getShare_image() {
return share_image;
}
public void setShare_image(String share_image) {
this.share_image = share_image;
}
public String getShare_link() {
return share_link;
}
public void setShare_link(String share_link) {
this.share_link = share_link;
}
}
複製代碼
微信分享工具類WXShareUtil代碼:api
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import com.example.jingbin.zkfudou.R;
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.WXMusicObject;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import java.io.ByteArrayOutputStream;
public class WXShareUtil {
private static IWXAPI api;
/**
* 微信文字分享
* @param text 分享內容
* @param context
*/
public static void WXTextShare(String text, Context context){
//初始化一個WXTextObject對象,填寫分享的文本內容
WXTextObject textObject = new WXTextObject();
textObject.text = text;
//用WXTextObject對象初始化一個WXMediaMessage對象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObject;
msg.description = text;
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
api.sendReq(req);
}
/**
* 微信分享圖片
* @param resId 圖片資源id
* @param context
*/
public static void WXBitmapShare(int resId, Context context){
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),resId);
//初始化一個WXImageObject對象和WXMediaMessage對象
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
//設置縮略圖
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,100,100,true);
bmp.recycle();
// msg.thumbData = ConvertUtil.ConvertBitmapToBytes(thumbBmp,true);
msg.thumbData = Util.bmpToByteArray(thumbBmp,true);
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");//transaction字段用於惟一標識一個請求
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
api.sendReq(req);
}
/**
* 微信分享圖片
* @param bitmap 圖片
* @param context
*/
public static void WXBitmapShare(Bitmap bitmap, Context context){
//初始化一個WXImageObject對象和WXMediaMessage對象
WXImageObject imgObj = new WXImageObject(bitmap);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
//設置縮略圖
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap,100,100,true);
bitmap.recycle();
// msg.thumbData = ConvertUtil.ConvertBitmapToBytes(thumbBmp,true);
msg.thumbData = Util.bmpToByteArray(thumbBmp,true);
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");//transaction字段用於惟一標識一個請求
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
api.sendReq(req);
}
private static String buildTransaction(String img) {
return System.currentTimeMillis()+img;
}
/**
* 微信分享音樂
* @param musicUrl
* @param context
*/
public static void WXMusicShare(String musicUrl, Context context){
//初始化一個WXMusicObject對象,填寫url
WXMusicObject music = new WXMusicObject();
music.musicUrl = musicUrl;
//用WXMusicObject初始化一個WXMediaMessage對象,填寫標題、描述
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = music;
msg.title = "音樂標題";
msg.description = "音樂描述";
//設置縮略圖
Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_head_default);
// msg.thumbData = ConvertUtil.ConvertBitmapToBytes(thumbBmp,true);
msg.thumbData = Util.bmpToByteArray(thumbBmp,true);
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("music");//transaction字段用於惟一標識一個請求
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
api.sendReq(req);
}
/**
* 微信分享視頻
* @param videoUrl
* @param context
*/
public static void WXVideoShare(String videoUrl, Context context){
//初始化一個WXVideoObject對象,填寫url
WXVideoObject video = new WXVideoObject();
video.videoUrl = videoUrl;
//用WXVideoObject初始化一個WXMediaMessage對象,填寫標題、描述
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = video;
msg.title = "視頻標題";
msg.description = "視頻描述";
//設置縮略圖
Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_head_default);
// msg.thumbData = ConvertUtil.ConvertBitmapToBytes(thumbBmp,true);
msg.thumbData = Util.bmpToByteArray(thumbBmp,true);
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("video");//transaction字段用於惟一標識一個請求
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
api.sendReq(req);
}
public static void WXWebpageShare(String webpageUrl,String title,String description,int imgRes, Context context){
// Log.e("WXShare","webpage share WXWebpageObject");
//初始化一個WXWebpageObject對象,填寫url
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = webpageUrl;
// Log.e("WXShare","webpage share WXMediaMessage");
//用WXWebpageObject初始化一個WXMediaMessage對象,填寫標題、描述
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = webpage;
msg.title = title;
msg.description = description;
// Log.e("WXShare","webpage share thumbBmp");
//設置縮略圖
// Bitmap bitmap= Glide.with(context).asBitmap().load(imgUrl);
//// Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_head_default);
//// msg.thumbData = ConvertUtil.bitmap2Bytes(thumbBmp,32*1024);
// if (bitmap != null) {
// msg.thumbData = Util.image2Bytes(imgUrl);
Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), imgRes);
// msg.thumbData = ConvertUtil.ConvertBitmapToBytes(thumbBmp,true);
msg.thumbData = Util.bmpToByteArray(thumbBmp,true);
// }
// Log.e("WXShare","webpage share req");
//構造一個Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");//transaction字段用於惟一標識一個請求
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
//分享到朋友圈:req.scene = SendMessageToWX.Req.WXSceneTimeline;
//分享到好友會話:req.scene = SendMessageToWX.Req.WXSceneSession;
//添加到微信收藏:req.scene = SendMessageToWX.Req.WXSceneFavorite;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
}
// Log.e("WXShare","webpage share sendReq");
api.sendReq(req);
}
/**
* 微信分享
* @param mContext
* @param url 地址
* @param title 標題
* @param desc 介紹
* @param isFriends 朋友圈或好友
* @param img 圖片
*/
public static void wxShare(Context mContext, String url, String title, String desc, boolean isFriends, Bitmap img) {
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = url;
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = title;
msg.description = desc;
msg.thumbData = bitmapBytes(img,true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
// req.scene = friendsCircle ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
//調用api接口發送數據到微信
if (api==null){
api = WXAPIFactory.createWXAPI(mContext, Constant.WEIXIN_APP_ID);
}
// Log.e("WXShare","webpage share sendReq");
api.sendReq(req);
// BaseApp.mWxApi.sendReq(req);
}
public static byte[] bitmapBytes(final Bitmap bmp, final boolean needRecycle) {
int i;
int j;
if (bmp.getHeight() > bmp.getWidth()) {
i = bmp.getWidth();
j = bmp.getWidth();
} else {
i = bmp.getHeight();
j = bmp.getHeight();
}
Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
Canvas localCanvas = new Canvas(localBitmap);
while (true) {
localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0,i, j), null);
if (needRecycle)
bmp.recycle();
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
localBitmap.recycle();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
try {
localByteArrayOutputStream.close();
return arrayOfByte;
} catch (Exception e) {
//F.out(e);
}
i = bmp.getHeight();
j = bmp.getHeight();
}
}
}
複製代碼