相信做app的都遇到要分享朋友圈,或者把東西分享給別人的這個功能,下面就是實現這個功能的代碼。
要是遇到什麼問題,在評論區留言,我會在第一時間回覆哦!!!
1、第一步添加依賴
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
implementation 'com.android.support:design:27.1.1'
2、寫一個分享的界面layout_popup_share.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bgcolor_tabbar"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<Button
android:id="@+id/bottom_share_qq"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableTop="qq圖片在最下面自己下載,自己導入吧,嘻嘻"
android:scaleType="center"
android:textColor="#666666"
android:text="QQ"
android:visibility="gone" />
<Button
android:id="@+id/bottom_share_qzone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawableTop="qq空間圖片在最下面自己下載,自己導入吧,嘻嘻"
android:scaleType="center"
android:textColor="#666666"
android:text="QQ空間"
android:visibility="gone" />
<Button
android:id="@+id/bottom_share_wechat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#666666"
android:layout_marginLeft="40dp"
android:background="@null"
android:drawableTop="微信圖片在最下面自己下載,自己導入吧,嘻嘻"
android:scaleType="center"
android:text="微信" />
<Button
android:id="@+id/bottom_share_wxcircle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="40dp"
android:textColor="#666666"
android:background="@null"
android:drawableTop="微信朋友圈圖片在最下面自己下載,自己導入吧,嘻嘻"
android:scaleType="center"
android:text="朋友圈" />
<Button
android:id="@+id/bottom_share_sina"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#666666"
android:background="@null"
android:drawableTop="新浪微博圖片在最下面自己下載,自己導入吧,嘻嘻"
android:scaleType="center"
android:text="微博"
android:visibility="gone" />
<TextView
android:id="@+id/version_name_tv"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
qq圖片下載
qq空間圖片下載
微信朋友圈圖片下載
微信圖片下載
新浪微博圖片下載
3、新建類WeChatShareUtil
import android.content.Context;
import android.graphics.Bitmap;
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.ByteArrayOutputStream;
public class WeChatShareUtil {
//從官網申請的合法appId
public static final String APP_ID = "xxxx";
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, APP_ID, true);
//將應用的appId註冊到微信
api.registerApp(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;
}
}
4、在項目文件夾中,創建一個 wxpi 文件夾,一定要是這個名字,大小寫都不能錯
新建類WXEntryActivity
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IWXAPI api = WXAPIFactory.createWXAPI(this, WeChatShareUtil.APP_ID, false);
api.handleIntent(getIntent(),this);
finish();
}
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
String result;
switch (baseResp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = "分享成功";
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
result = null;
break;
default:
result = "分享失敗";
break;
}
if (result != null) {
// Toast.makeText(this, baseResp.errCode + result, Toast.LENGTH_SHORT).show();
}
}
}
5、在需要點擊分享的地方,添加分享的點擊事件
BottomSheetDialog shareDialog;
shareDialog = new BottomSheetDialog(MineWebViewActivity.this, R.style.BottomDialog);
View view = View.inflate(MineWebViewActivity.this, R.layout.layout_popup_share, null);
Button wechat = view.findViewById(R.id.bottom_share_wechat);
wechat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("share", "nativeshare 微信分享 ");
if (weChatShareUtil.isSupportWX()) {
String desc = "百度";
boolean result = true;
String url ="http://www.baidu.com";
String title="百度";
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo);//添加一個logo圖片就可以了
result = weChatShareUtil.shareUrl(url,title,bitmap,desc, SendMessageToWX.Req.WXSceneSession);
} else {
Toast.makeText(MineWebViewActivity.this, "手機上微信版本不支持分享到朋友圈", Toast.LENGTH_SHORT).show();
}
}
});
Button wxcircle = view.findViewById(R.id.bottom_share_wxcircle);
wxcircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("share", "nativeshare 微信朋友圈分享 ");
if (weChatShareUtil.isSupportWX()) {
String desc = "百度";
boolean result = true;
String url ="http://www.baidu.com";
String title="百度";
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo);
result = weChatShareUtil.shareUrl(url,title,bitmap,desc, SendMessageToWX.Req.WXSceneTimeline);
} else {
Toast.makeText(MineWebViewActivity.this, "手機上微信版本不支持分享到朋友圈", Toast.LENGTH_SHORT).show();
}
}
});
shareDialog.setContentView(view);
shareDialog.show();