写点什么

ShareSDK Android 端主流平台分享示例

  • 2022 年 9 月 06 日
    上海
  • 本文字数:5292 字

    阅读完需:约 17 分钟

ShareSDK 可以调用 getPlatform 接口设置要分享的平台,下面是分享示例:

微信分享示例(好友、朋友圈、收藏)

微信分享文本

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_TEXT);shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");platform.share(shareParams);
复制代码

微信分享图片

注意:imageUrl(”网络图片链接”)、imagePath(“/sdcard/abc.jpg”)、imageData(bitmap)是三选一,同时 Android11 及之后的版本,微信分享图片不支持用 imageData(bitmap)接口设置参数;

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_IMAGE);//分享网络图片shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")//分享本地图片//shareParams.setImagePath("本地图片的路径");//分享bitmap图片//shareParams.setImageData(bitmap);platform.share(shareParams);
复制代码

微信分享音乐

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_MUSIC);shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")shareParams.setMusicUrl( "http://music.baidu.com/song/546920050?pst=sug"  );shareParams.setUrl("http://music.baidu.com/song/546920050?pst=su");platform.share(shareParams);
复制代码

微信分享视频

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_VIDEO);shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")shareParams.setUrl("http://f1.webshare.mob.com/dvideo/demovideos.mp4");platform.share(shareParams);
复制代码

微信分享网页

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_WEBPAGE);shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")shareParams.setUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

微信分享文件

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_FILE);shareParams.setTitle("测试分享的标题");shareParams.setFilePath("/storage/emulated/0/Android/data/cn.sharesdk.demo/files/text.txt");//文件本地路径platform.share(shareParams);
复制代码

微信分享小程序

注意:只有微信好友支持分享小程序

Platform platform = ShareSDK.getPlatform(Wechat.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();//分享卡片形式小程序shareParams.setShareType(Platform.SHARE_WXMINIPROGRAM);//直接打开微信小程序//shareParams.setShareType(Platform.OPEN_WXMINIPROGRAM);shareParams.setText("测试文本");shareParams.setTitle("测试分享的标题");shareParams.setUrl("http://sharesdk.cn");shareParams.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");//小程序状态:0-正式,1-开发,2-体验shareParams.setWxMiniProgramType(0);  //微信小程序多选,默认只能分享一个微信好友,设置false后可以分享多个shareParams.setWxWithShareTicket(false);//分享的小程序页面路径,如不需要指定,请填主页路径(必填)shareParams.setWxPath("pages/index/index");shareParams.setWxUserName("gh_52568203455c");platform.share(shareParams);
复制代码

新浪微博分享示例

新浪微博分享文字

Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");platform.share(shareParams);
复制代码

新浪微博分享图文

注意:imageUrl(”网络图片链接”)、imagePath(“/sdcard/abc.jpg”)、imageData(bitmap)是三选一

Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");//分享网络图片shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")//分享本地图片//shareParams.setImagePath("本地图片的路径");//分享bitmap图片//shareParams.setImageData(bitmap);platform.share(shareParams);
复制代码

新浪微博分享本地视频

Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");//视频文件本地路径shareParams.setFilePath("/sdcard/video.MP4");platform.share(shareParams);
复制代码

QQ 分享示例

QQ 分享图片

注意:imageUrl(”网络图片链接”)、imagePath(“/sdcard/abc.jpg”)是二选一,同时 QQ 不支持分享 bitmap 图片

Platform platform = ShareSDK.getPlatform(QQ.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();//分享网络图片shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")//分享本地图片//shareParams.setImagePath("本地图片的路径");platform.share(shareParams);
复制代码

QQ 分享网页

Platform platform = ShareSDK.getPlatform(QQ.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")//网页链接shareParams.setTitleUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

QQ 分享音频

Platform platform = ShareSDK.getPlatform(QQ.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setTitle("测试分享的标题");shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg")shareParams.setTitleUrl("https://www.mob.com/");//网页链接shareParams.setMusicUrl( "http://music.baidu.com/song/546920050?pst=sug"  );//音乐链接platform.share(shareParams);
复制代码

QQ 分享小程序

Platform platform = ShareSDK.getPlatform(QQ.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("QQ小程序");shareParams.setTitle("QQ互联");shareParams.setTitleUrl("http://www.qq.com/");shareParams.setImageUrl("http://www.3wyu.com/wpcontent/uploads/6e0eaf15gy1fvr5tnm2dfj20f108gtad.jpg");shareParams.setShareType(Platform.QQ_MINI_PROGRAM);//分享小程序appidshareParams.setQQMiniProgramAppid("1109762672"); //分享的小程序页面路径,如不需要指定,请填主页路径(必填)shareParams.setQQMiniProgramPath("pages/index/index");//3表示正式版,1表示体验版(可选)//shareParams.setQQMiniProgramType("");platform.share(shareParams);
复制代码

QQ 空间分享示例

注意:必须需要 QQ 客户端才可以分享, site 是分享此内容的网站名称,仅在 QQ 空间使用;siteUrl 是分享此内容的网站地址,仅在 QQ 空间使用;

QQ 空间分享视频

Platform platform = ShareSDK.getPlatform(QZone.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_VIDEO);//视频文件本地路径shareParams.setFilePath("/sdcard/video.MP4");platform.share(shareParams);
复制代码

QQ 空间分享文本

Platform platform = ShareSDK.getPlatform(QZone.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setTitle("测试分享的标题");//网页链接shareParams.setTitleUrl("https://www.mob.com/");shareParams.setText("测试文本");shareParams.setSite("测试文本");shareParams.setSiteUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

QQ 空间分享说说

Platform platform = ShareSDK.getPlatform(QZone.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg");shareParams.setSite("测试文本");shareParams.setSiteUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

QQ 空间分享图文

Platform platform = ShareSDK.getPlatform(QZone.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setTitle("测试分享的标题");shareParams.setTitleUrl("https://www.mob.com/");shareParams.setText("测试文本");//本地图片路径shareParams.setImagePath("/storage/emulated/0/DCIM/Camera/IMG_20191001_113538.jpg");shareParams.setSite("测试文本");shareParams.setSiteUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

Facebook 分享示例

Facebook 分享网页

Platform platform = ShareSDK.getPlatform(Facebook.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setQuote("测试web引文分享");shareParams.setUrl("https://www.mob.com/");platform.share(shareParams);
复制代码

Facebook 分享图片

Platform platform = ShareSDK.getPlatform(Facebook.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setHashtag("测试话题分享");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg");platform.share(shareParams);
复制代码

Facebook 分享视频

Platform platform = ShareSDK.getPlatform(Facebook.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();//视频文件本地路径shareParams.setFilePath("/sdcard/video.MP4");platform.share(shareParams);
复制代码

Facebook 分享多图

Platform platform = ShareSDK.getPlatform(Facebook.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();//图片数组shareParams.setImageArray(AVATARS);platform.share(shareParams);
复制代码

Twitter 分享示例

Twitter 分享文本

Platform platform = ShareSDK.getPlatform(Twitter.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");platform.share(shareParams);
复制代码

Twitter 分享图文

Platform platform = ShareSDK.getPlatform(Twitter.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg");platform.share(shareParams);
复制代码

Twitter 分享视频

Platform platform = ShareSDK.getPlatform(Twitter.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_VIDEO);//视频文件本地路径shareParams.setFilePath("/sdcard/video.MP4");platform.share(shareParams);
复制代码

Instagram 分享示例

Instagram 分享图片

Platform platform = ShareSDK.getPlatform(Twitter.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();//文本不显示shareParams.setText("测试文本");shareParams.setImageUrl("https://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg");platform.share(shareParams);
复制代码

Instagram 分享图片

Platform platform = ShareSDK.getPlatform(Twitter.NAME);Platform.ShareParams shareParams = new Platform.ShareParams();shareParams.setShareType(Platform.SHARE_VIDEO);//文本不显示shareParams.setText("测试文本");//视频文件本地路径shareParams.setFilePath("/sdcard/video.MP4");platform.share(shareParams);
复制代码


用户头像

还未添加个人签名 2019.05.08 加入

还未添加个人简介

评论

发布
暂无评论
ShareSDK Android端主流平台分享示例_an'droid_MobTech袤博科技_InfoQ写作社区