相信做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();