本文对应的Git项目
本文主要介绍以下功能:
- 分享(纯文本|图片|文件)给QQ好友
- QZone(QQ空间)-分享到“QQ空间”应用-目前QQ不支持分享到QQ中自带空间
- 新浪微博分享-分两种(1)分享给好友;(2)分享到微博内容
- 直接分享(纯文本|图片|文件)给微信好友
- 直接分享图片到微信朋友圈
注意-更新于19年5月23日:
1.微信在6.6.7版本以后,已经不支持往朋友圈分享标题了,具体请点击查看
2.微信在6.7.3版本以后,已经不支持往朋友圈分享多图了,具体请点击查看 - android7.0以上系统获取url的方法
- 保存图片
- 直接添加进入QQ群
简单说明-也是我当时需要要做出来的效果:
1.QQ:分享到QQ好友时,直接以发文本的方式分享出去,字数不受限制
2.WechatMoment : 分享到微信朋友圈时,需要将图片带到发送朋友圈图文发送界面直接发送分享(新版微信不支持同时带文本)
3.直接分享图片给微信好友
说到分享许多人都会使用一些第三方sdk来实现,比如“友盟分享”,“Mob分享(ShareSdk)”。
这两种分享方式需要配置许多文件和代码,虽说省去了我们一个个集成各大网站分享的sdk,但常常也会自带许多坑,具体的大家在使用的时候就会发现,我就不评价了。
QQ分享:无论是友盟或sharesdk,都无法实现直接将长文本分享到QQ好友,原因是什么?我找过他们客服,这是tencent(腾讯)公司分享sdk规定的。具体可查看:链接中的 1.13 分享消息到QQ(无需QQ登录)的参数,以QQapi上的代码进行分享以后除了文本字数有限制以外,而且其分享的样式是以链接的形式分享出去的,所以不符合我的要求。
WechatMoment微信朋友圈分享:以官方文档或以第三方通道分享时,在分享到朋友圈的时候,分享文本是设置不到文本区域的,无论你用啥方法。因为ShareSdk官方文档也说了:查看第4点(微信(好友、朋友圈、收藏))当你需要同时设置分享图片和文本时你设置的text是不显示的。
我的解决方案:(使用Intent分享)但有个缺点是,分享到平台后却不能返回到原应用,只能留在QQ或微信(除在微信分享文件-分享文件给好友可以返回到原应用)
先给大家提供个判断QQ向微信客户端是否存在的两个方法:
调用方式如:Platformutil.isInstallApp(context,PlatformUtil.PACKAGE_WECHAT);// 判断微信是否安装
<code class=" language-dart">public <span class="token keyword">class</span> <span class="token class-name">PlatformUtil</span> <span class="token punctuation">{</span> public <span class="token keyword">static</span> <span class="token keyword">final</span> String PACKAGE_WECHAT <span class="token operator">=</span> <span class="token string">"com.tencent.mm"</span><span class="token punctuation">;</span> public <span class="token keyword">static</span> <span class="token keyword">final</span> String PACKAGE_MOBILE_QQ <span class="token operator">=</span> <span class="token string">"com.tencent.mobileqq"</span><span class="token punctuation">;</span> public <span class="token keyword">static</span> <span class="token keyword">final</span> String PACKAGE_QZONE <span class="token operator">=</span> <span class="token string">"com.qzone"</span><span class="token punctuation">;</span> public <span class="token keyword">static</span> <span class="token keyword">final</span> String PACKAGE_SINA <span class="token operator">=</span> <span class="token string">"com.sina.weibo"</span><span class="token punctuation">;</span> <span class="token comment">// 判断是否安装指定app</span> public <span class="token keyword">static</span> boolean <span class="token function">isInstallApp</span><span class="token punctuation">(</span>Context context<span class="token punctuation">,</span> String app_package<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">final</span> PackageManager packageManager <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">getPackageManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> List<span class="token operator"><</span>PackageInfo<span class="token operator">></span> pInfo <span class="token operator">=</span> packageManager<span class="token punctuation">.</span><span class="token function">getInstalledPackages</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>pInfo <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>int i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> pInfo<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String pn <span class="token operator">=</span> pInfo<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">.</span>packageName<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>app_package<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>pn<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
QQ分享(分享至QQ好友或群组):
<code class=" language-dart"> <span class="token comment">/** * 直接分享纯文本内容至QQ好友 * @param mContext * @param content */</span> public <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">shareQQ</span><span class="token punctuation">(</span>Context mContext<span class="token punctuation">,</span> String content<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Platformutil<span class="token punctuation">.</span><span class="token function">isInstallApp</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_MOBILE_QQ<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token string">"android.intent.action.SEND"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"text/plain"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_SUBJECT<span class="token punctuation">,</span> <span class="token string">"分享"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_TEXT<span class="token punctuation">,</span> content<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setFlags</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>FLAG_ACTIVITY_NEW_TASK<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span><span class="token string">"com.tencent.mobileqq"</span><span class="token punctuation">,</span> <span class="token string">"com.tencent.mobileqq.activity.JumpActivity"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> mContext<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span> <span class="token string">"您需要安装QQ客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">/** * 分享图片给QQ好友 * * @param bitmap */</span> public <span class="token keyword">void</span> <span class="token function">shareImageToQQ</span><span class="token punctuation">(</span>Bitmap bitmap<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Platformutil<span class="token punctuation">.</span><span class="token function">isInstallApp</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_MOBILE_QQ<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Uri uriToImage <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>MediaStore<span class="token punctuation">.</span>Images<span class="token punctuation">.</span>Media<span class="token punctuation">.</span><span class="token function">insertImage</span><span class="token punctuation">(</span> mContext<span class="token punctuation">.</span><span class="token function">getContentResolver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> bitmap<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Intent shareIntent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> shareIntent<span class="token punctuation">.</span><span class="token function">setAction</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>ACTION_SEND<span class="token punctuation">)</span><span class="token punctuation">;</span> shareIntent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uriToImage<span class="token punctuation">)</span><span class="token punctuation">;</span> shareIntent<span class="token punctuation">.</span><span class="token function">setFlags</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>FLAG_ACTIVITY_NEW_TASK<span class="token punctuation">)</span><span class="token punctuation">;</span> shareIntent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"image/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 遍历所有支持发送图片的应用。找到需要的应用</span> ComponentName componentName <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span><span class="token string">"com.tencent.mobileqq"</span><span class="token punctuation">,</span> <span class="token string">"com.tencent.mobileqq.activity.JumpActivity"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> shareIntent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span>componentName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">/</span> mContext<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>shareIntent<span class="token punctuation">)</span><span class="token punctuation">;</span> mContext<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span><span class="token function">createChooser</span><span class="token punctuation">(</span>shareIntent<span class="token punctuation">,</span> <span class="token string">"Share"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">/</span><span class="token operator">/</span> ContextUtil<span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">showToastMsg</span><span class="token punctuation">(</span><span class="token string">"分享图片到**失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> 之前有同学说在分享QQ和微信的时候,发现只要QQ或微信在打开的情况下,再调用分享只是打开了QQ和微信,却没有调用选择分享联系人的情况,这里,我要感觉一下@<span class="token punctuation">[</span>努力搬砖<span class="token punctuation">]</span>同学,他找出了原因。 解决办法如下: mActivity<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>如果微信或者QQ已经唤醒或者打开,这样只能唤醒微信,不能分享 请使用 mActivity<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span><span class="token function">createChooser</span><span class="token punctuation">(</span>intent<span class="token punctuation">,</span> <span class="token string">"Share"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </code>
QZone(QQ空间)
经过我反复试验,通过adb(方法:adb shell “dumpsys window | grep mCurrentFocus”)查找QQ中的空间发布说说界面是:cooperation.qzone.QzonePublishMoodProxyActivity,但始终歪能成功,在网上看到有人说是该界面没有对外开放,所以这里只能通过调用qq空间app打开发布界面进行分享内容!(强调:不是QQ内自带发布界面,是QQ空间单独的APP)。后期再研究下能否直接使用QQ内直接的界面。
<code class=" language-dart"><span class="token comment">/** * description : 分享到QQ空间 * created at 2018/7/9 17:04 * * @param photoPath 照片路径 */</span> public <span class="token keyword">void</span> <span class="token function">shareImageToQQZone</span><span class="token punctuation">(</span>String photoPath<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span><span class="token function">isInstalledSpecifiedApp</span><span class="token punctuation">(</span>mActivity<span class="token punctuation">,</span> PlatformUtil<span class="token punctuation">.</span>PACKAGE_QZONG<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>photoPath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String tip <span class="token operator">=</span> <span class="token string">"文件不存在"</span><span class="token punctuation">;</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>mActivity<span class="token punctuation">,</span> tip <span class="token operator">+</span> <span class="token string">" path = "</span> <span class="token operator">+</span> photoPath<span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ComponentName componentName <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_QZONG<span class="token punctuation">,</span> PlatformUtil<span class="token punctuation">.</span>ACTIVITY_SHARE_QQ_ZONE<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span>componentName<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setAction</span><span class="token punctuation">(</span><span class="token string">"android.intent.action.SEND"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"image/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_TEXT<span class="token punctuation">,</span> <span class="token string">"I'm so tired!!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Uri uri<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Build<span class="token punctuation">.</span>VERSION<span class="token punctuation">.</span>SDK_INT <span class="token operator">>=</span> Build<span class="token punctuation">.</span>VERSION_CODES<span class="token punctuation">.</span>N<span class="token punctuation">)</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> FileProvider<span class="token punctuation">.</span><span class="token function">getUriForFile</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span> ShareToolUtil<span class="token punctuation">.</span>AUTHORITY<span class="token punctuation">,</span> file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">fromFile</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> mActivity<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>mActivity<span class="token punctuation">,</span> <span class="token string">"您需要安装QQ空间客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
Wechat(微信好友)
<code class=" language-dart"><span class="token comment">/** * 直接分享图片到微信好友 * @param context * @param picFile */</span> public <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">shareWechatFriend</span><span class="token punctuation">(</span>Context context<span class="token punctuation">,</span>String content <span class="token punctuation">,</span>File picFile<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Platformutil<span class="token punctuation">.</span><span class="token function">isInstallApp</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_WE_CHAT<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ComponentName cop <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span><span class="token string">"com.tencent.mm"</span><span class="token punctuation">,</span><span class="token string">"com.tencent.mm.ui.tools.ShareImgUI"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span>cop<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setAction</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>ACTION_SEND<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"image/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>picFile <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>picFile<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> picFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Uri uri<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Build<span class="token punctuation">.</span>VERSION<span class="token punctuation">.</span>SDK_INT <span class="token operator">>=</span> Build<span class="token punctuation">.</span>VERSION_CODES<span class="token punctuation">.</span>N<span class="token punctuation">)</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> FileProvider<span class="token punctuation">.</span><span class="token function">getUriForFile</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> ShareToolUtil<span class="token punctuation">.</span>AUTHORITY<span class="token punctuation">,</span> picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">fromFile</span><span class="token punctuation">(</span>picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri);</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">/</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span><span class="token string">"Kdescription"</span><span class="token punctuation">,</span> <span class="token operator">!</span>TextUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span> <span class="token operator">?</span> content <span class="token punctuation">:</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setFlags</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>FLAG_ACTIVITY_NEW_TASK<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">/</span> context<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span><span class="token function">createChooser</span><span class="token punctuation">(</span>intent<span class="token punctuation">,</span> <span class="token string">"Share"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token string">"您需要安装微信客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">/** * 直接分享文本到微信好友 * * @param context 上下文 */</span> public <span class="token keyword">void</span> <span class="token function">shareWechatFriend</span><span class="token punctuation">(</span>Context context<span class="token punctuation">,</span> String content<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Platformutil<span class="token punctuation">.</span><span class="token function">isInstallApp</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_WE_CHAT<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ComponentName cop <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span><span class="token string">"com.tencent.mm"</span><span class="token punctuation">,</span> <span class="token string">"com.tencent.mm.ui.tools.ShareImgUI"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span>cop<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setAction</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>ACTION_SEND<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span><span class="token string">"android.intent.extra.TEXT"</span><span class="token punctuation">,</span> content<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// intent.putExtra("sms_body", content);</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span><span class="token string">"Kdescription"</span><span class="token punctuation">,</span> <span class="token operator">!</span>TextUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span> <span class="token operator">?</span> content <span class="token punctuation">:</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setFlags</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>FLAG_ACTIVITY_NEW_TASK<span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token string">"您需要安装微信客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
WechatMoment(微信朋友圈)分享:
(这段更新时间:19-05-23)可查看文章开头更新内容,这里说一下解决方法:在不能分享标题到朋友圈的时候,可以在点击分享按钮时,将需要分享的内容复制到剪贴板,并toast给用户到微信朋友圈发布界面粘帖。
<code class=" language-dart"> <span class="token comment">/** * 直接分享文本和图片到微信朋友圈 * @param context * @param content */</span> public <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">shareWechatMoment</span><span class="token punctuation">(</span>Context context<span class="token punctuation">,</span> String content<span class="token punctuation">,</span> File picFile<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span> <span class="token function">isInstallApp</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_WECHAT<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//分享精确到微信的页面,朋友圈页面,或者选择好友分享页面</span> ComponentName comp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ComponentName</span><span class="token punctuation">(</span><span class="token string">"com.tencent.mm"</span><span class="token punctuation">,</span> <span class="token string">"com.tencent.mm.ui.tools.ShareToTimeLineUI"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setComponent</span><span class="token punctuation">(</span>comp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// intent.setAction(Intent.ACTION_SEND_MULTIPLE);// 分享多张图片时使用</span> intent<span class="token punctuation">.</span><span class="token function">setAction</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>ACTION_SEND<span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"image/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//添加Uri图片地址--用于添加多张图片</span> <span class="token operator">/</span><span class="token operator">/</span>ArrayList<span class="token operator"><</span>Uri<span class="token operator">></span> imageUris <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">/</span>intent<span class="token punctuation">.</span><span class="token function">putParcelableArrayListExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> imageUris<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>picFile <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>picFile<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> picFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Uri uri<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Build<span class="token punctuation">.</span>VERSION<span class="token punctuation">.</span>SDK_INT <span class="token operator">>=</span> Build<span class="token punctuation">.</span>VERSION_CODES<span class="token punctuation">.</span>N<span class="token punctuation">)</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> FileProvider<span class="token punctuation">.</span><span class="token function">getUriForFile</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> ShareToolUtil<span class="token punctuation">.</span>AUTHORITY<span class="token punctuation">,</span> picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">fromFile</span><span class="token punctuation">(</span>picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">/</span> 微信现不能进行标题同时分享 <span class="token operator">/</span><span class="token operator">/</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span><span class="token string">"Kdescription"</span><span class="token punctuation">,</span> <span class="token operator">!</span>TextUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span> <span class="token operator">?</span> content <span class="token punctuation">:</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setFlags</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>FLAG_ACTIVITY_NEW_TASK<span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token string">"您需要安装微信客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
在上面微信好友和朋友圈分享的过程中遇到了这样一串代码:
<code class=" language-dart"> <span class="token keyword">if</span> <span class="token punctuation">(</span>picFile<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> picFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Uri uri<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Build<span class="token punctuation">.</span>VERSION<span class="token punctuation">.</span>SDK_INT <span class="token operator">>=</span> Build<span class="token punctuation">.</span>VERSION_CODES<span class="token punctuation">.</span>N<span class="token punctuation">)</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> FileProvider<span class="token punctuation">.</span><span class="token function">getUriForFile</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> ShareToolUtil<span class="token punctuation">.</span>AUTHORITY<span class="token punctuation">,</span> picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">fromFile</span><span class="token punctuation">(</span>picFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri);</span> <span class="token punctuation">}</span> </code>
上面这部分代码主要功能是判断了下文件是否存在,在android版本高过7.0(包括7.0版本)当前APP是不能直接向外部应用提供file开头的的文件路径,需要通过FileProvider转换一下。否则在7.0及以上版本手机将直接crash。
这里再提供下我这里具体的逻辑:
<code class=" language-dart">常量:ShareToolUtil<span class="token punctuation">.</span>AUTHORITY <span class="token operator">--</span>写在哪儿无所谓,只要能调用到 public <span class="token keyword">static</span> <span class="token keyword">final</span> String AUTHORITY <span class="token operator">=</span> <span class="token string">"com.gudd.demo.fileprovider"</span><span class="token punctuation">;</span> </code>
<code class=" language-jsx"><span class="token comment">// AndroidManifest需要加一个provider的标签,里面的authorities就是我们上面需要调用的一个常量</span> <span class="token comment">// 如果你有多个module的话也没关系,所有的module只要有一个地方写了这个就行,因为app打包以后所有的AndroidManifest文件将会合并到一起</span> <span class="token operator"><</span>provider android<span class="token punctuation">:</span>name<span class="token operator">=</span><span class="token string">"android.support.v4.content.FileProvider"</span> <span class="token comment">// 再说一遍哦:这里的"com.gudd.demo"是自定义的,需要改成你们自己的名字,其实可以随便写啦。这里很重要,不能两个app使用相同的名字,否则~~嘿嘿,两个app是不能同时安装的!!</span> android<span class="token punctuation">:</span>authorities<span class="token operator">=</span><span class="token string">"com.gudd.demo.fileprovider"</span> android<span class="token punctuation">:</span>exported<span class="token operator">=</span><span class="token string">"false"</span> android<span class="token punctuation">:</span>grantUriPermissions<span class="token operator">=</span><span class="token string">"true"</span><span class="token operator">></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta-data</span> <span class="token attr-name"><span class="token namespace">android:</span>name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>android.support.FILE_PROVIDER_PATHS<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@xml/file_paths_screencapture<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>provider</span><span class="token punctuation">></span></span> </code>
上面还有个资源文件:resource=”@xml/file_paths_screencapture”
<code class=" language-xml">// 上图是写这个文件要放的位置,随便哪个Module都没关系 // 只要AndroidManifest里面能直接调用到就可以。但要记得写在xml里面。 <span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>paths</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>root-path</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ext_sd_files<span class="token punctuation">"</span></span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>files-path</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>int_files<span class="token punctuation">"</span></span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>.<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>external-path</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ext_files<span class="token punctuation">"</span></span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>cache-path</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>cache_files<span class="token punctuation">"</span></span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>external-files-path</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>ext_int_files<span class="token punctuation">"</span></span> <span class="token attr-name">path</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>paths</span><span class="token punctuation">></span></span> // 可能上面这串代码放到文件中会有些红色的波浪线,呵呵,但又有啥关系呢,能运行起来不就行了,哈哈 </code>
这里再提供一下我保存图片的一个方法:
<code class=" language-csharp"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ShareToolUtil</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> sharePicName <span class="token operator">=</span> <span class="token string">"share_pic.jpg"</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">String</span> sharePicPath <span class="token operator">=</span> Environment<span class="token punctuation">.</span><span class="token function">getExternalStorageDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span> File<span class="token punctuation">.</span>separator<span class="token operator">+</span><span class="token string">"UmeBrowser"</span><span class="token operator">+</span>File<span class="token punctuation">.</span>separator<span class="token operator">+</span><span class="token string">"sharepic"</span><span class="token operator">+</span>File<span class="token punctuation">.</span>separator<span class="token punctuation">;</span> <span class="token comment">/** * 保存图片,并返回一个File类型的文件 * 如今Android版本已经高达28了,但在使用该方法时,涉及到权限问题,本人在创建文件夹时遇到文件夹创建失败问题,遂将原因及解决方法记录如下: 问题:Android6.0以后,文件夹创建失败。也就是使用file.mkdirs方法. 解决方法:1.读写sdcard需要权限,但仅在manifest.xml里面添加是不够的,需要动态申请权限。2.可以将targetSdkVersion改为21或22或以下。 否则在分享过程中获取不到图片就会弹出“获取资源失败”这样的提示。 */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">File</span> <span class="token function">saveSharePic</span><span class="token punctuation">(</span><span class="token class-name">Context</span> context<span class="token punctuation">,</span> <span class="token class-name">Bitmap</span> bitmap<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>FileUtil<span class="token punctuation">.</span><span class="token function">isSDcardExist</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>sharePicPath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> file<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token class-name">File</span> filePic <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>sharePicPath<span class="token punctuation">,</span>sharePicName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>filePic<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> filePic<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token class-name">FileOutputStream</span> <span class="token keyword">out</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>filePic<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>bitmap <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> bitmap <span class="token operator">=</span> BitmapFactory<span class="token punctuation">.</span><span class="token function">decodeResource</span><span class="token punctuation">(</span>context<span class="token punctuation">.</span><span class="token function">getResources</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> R<span class="token punctuation">.</span>drawable<span class="token punctuation">.</span>share_homepage<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> bitmap<span class="token punctuation">.</span><span class="token function">compress</span><span class="token punctuation">(</span>Bitmap<span class="token punctuation">.</span>CompressFormat<span class="token punctuation">.</span>PNG<span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">,</span> <span class="token keyword">out</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token keyword">out</span><span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">out</span><span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> filePic<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
这就是我的方法,在分享微信朋友圈的时候需要注意一点,分享的图片要保存在微信可获取到的目录下,一定不能保存在/data/data/****这个内置目录中,否则将获取不到图片报“获取不到图片资源,…..”导致分享失败。
后面如果有时间我会再将新浪的分享给写进来。
今天是7月10日,距离这篇文章已经有段时间了,为了说到做到,我把新浪的也给分享出来,还是在回头看这篇文章的时候想到,原来忘记我说的新浪这回事儿了,哈哈。。。
新浪微博分享
新浪分享分为两部分,一部分指分享给好友,一部分分享到内容。看下面吧。
<code class=" language-dart"><span class="token comment">/** * description : 微博分享 * created at 2018/7/10 13:58 */</span> public <span class="token keyword">void</span> <span class="token function">shareToSinaFriends</span><span class="token punctuation">(</span>Context context<span class="token punctuation">,</span>boolean isFriends<span class="token punctuation">,</span> String photoPath<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span><span class="token function">isInstalledSpecifiedApp</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> PlatformUtil<span class="token punctuation">.</span>PACKAGE_SINA<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>photoPath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String tip <span class="token operator">=</span> <span class="token string">"文件不存在"</span><span class="token punctuation">;</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> tip <span class="token operator">+</span> <span class="token string">" path = "</span> <span class="token operator">+</span> photoPath<span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>ACTION_SEND<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 使用以下两种type有一定的区别,"text/plain"分享给指定的粉丝或好友 ;"image/*"分享到微博内容</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>isFriends<span class="token punctuation">)</span> <span class="token punctuation">{</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"text/plain"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> intent<span class="token punctuation">.</span><span class="token function">setType</span><span class="token punctuation">(</span><span class="token string">"image/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 分享文本|文本+图片|图片 到微博内容时使用</span> <span class="token punctuation">}</span> PackageManager packageManager <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">getPackageManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> List<span class="token operator"><</span>ResolveInfo<span class="token operator">></span> matchs <span class="token operator">=</span> packageManager<span class="token punctuation">.</span><span class="token function">queryIntentActivities</span><span class="token punctuation">(</span>intent<span class="token punctuation">,</span> PackageManager<span class="token punctuation">.</span>MATCH_DEFAULT_ONLY<span class="token punctuation">)</span><span class="token punctuation">;</span> ResolveInfo resolveInfo <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>ResolveInfo each <span class="token punctuation">:</span> matchs<span class="token punctuation">)</span> <span class="token punctuation">{</span> String pkgName <span class="token operator">=</span> each<span class="token punctuation">.</span>activityInfo<span class="token punctuation">.</span>applicationInfo<span class="token punctuation">.</span>packageName<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_SINA<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>pkgName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> resolveInfo <span class="token operator">=</span> each<span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">// type = "image/*"--->com.sina.weibo.composerinde.ComposerDispatchActivity</span> <span class="token comment">// type = "text/plain"--->com.sina.weibo.weiyou.share.WeiyouShareDispatcher</span> intent<span class="token punctuation">.</span><span class="token function">setClassName</span><span class="token punctuation">(</span>PlatformUtil<span class="token punctuation">.</span>PACKAGE_SINA<span class="token punctuation">,</span> resolveInfo<span class="token punctuation">.</span>activityInfo<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 这里在使用resolveInfo的时候需要做判空处理防止crash</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_TEXT<span class="token punctuation">,</span> <span class="token string">"Test Text String !!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Uri uri<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Build<span class="token punctuation">.</span>VERSION<span class="token punctuation">.</span>SDK_INT <span class="token operator">>=</span> Build<span class="token punctuation">.</span>VERSION_CODES<span class="token punctuation">.</span>N<span class="token punctuation">)</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> FileProvider<span class="token punctuation">.</span><span class="token function">getUriForFile</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span> ShareToolUtil<span class="token punctuation">.</span>AUTHORITY<span class="token punctuation">,</span> file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> uri <span class="token operator">=</span> Uri<span class="token punctuation">.</span><span class="token function">fromFile</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> intent<span class="token punctuation">.</span><span class="token function">putExtra</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span>EXTRA_STREAM<span class="token punctuation">,</span> uri<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> context<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>mContext<span class="token punctuation">,</span> <span class="token string">"您需要安装sina客户端"</span><span class="token punctuation">,</span> Toast<span class="token punctuation">.</span>LENGTH_LONG<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
直接添加进入QQ群
之前有小伙伴问我如何实现QQ群直接加入功能,这里我简单给大家介绍一下,其实很简单,登录腾讯https://qun.qq.com/join.html就明白了。下面我贴下代码:
<code class=" language-dart"><span class="token comment">/** * 该群目前为我个人新建测试群,无聊的小盆友也可以加入进来壮大我的号,哈哈 * 发起添加群流程。群号:Android学习交流(610194891) 的 key 为: CXaQmSGNixYtgpaRuUlxd0CwyMhQYkd_ * 调用 joinQQGroup(CXaQmSGNixYtgpaRuUlxd0CwyMhQYkd_) 即可发起手Q客户端申请加群 Android学习交流(610194891) * * @param key 由官网生成的key * @return 返回true表示呼起手Q成功,返回fals表示呼起失败 */</span> public boolean <span class="token function">joinQQGroup</span><span class="token punctuation">(</span>String key<span class="token punctuation">)</span> <span class="token punctuation">{</span> Intent intent <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Intent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> intent<span class="token punctuation">.</span><span class="token function">setData</span><span class="token punctuation">(</span>Uri<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">"mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D"</span> <span class="token operator">+</span> key<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 此Flag可根据具体产品需要自定义,如设置,则在加群界面按返回,返回手Q主界面,不设置,按返回会返回到呼起产品界面 //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 未安装手Q或安装的版本不支持</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </code>
以上仅是个人工作中遇到的一些问题,记录下来方便以后查阅,如有优化方法请留言。
QQ、WECHAT在打开情况下分享时只能唤醒应用却不能调用分享联系人列表情况。解决办法如下:
<code class=" language-cpp">调用开启分享界面 mActivity<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>intent<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果微信或者QQ已经唤醒或者打开,这样只能唤醒微信,不能分享 </span> 请使用:mActivity<span class="token punctuation">.</span><span class="token function">startActivity</span><span class="token punctuation">(</span>Intent<span class="token punctuation">.</span><span class="token function">createChooser</span><span class="token punctuation">(</span>intent<span class="token punctuation">,</span> <span class="token string">"Share"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code>