欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android分享纯图片到QQ空间实现方式

程序员文章站 2023-10-30 14:39:52
最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,qq,qq空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型等,要求分享出去的内容不能带有当...

最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,qq,qq空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型等,要求分享出去的内容不能带有当前app的logo,而无论使用微信分享sdk,还是qq分享sdk,图文类型的分享都会带有当前app的logo和名称,所以笔者最终只能使用android原生实现分享功能了。

一.分享微信,分享微信单独分享一张图片时,可以使用原生分享,也可以使用微信分享sdk,sdk实现方式,笔者不再多述,网上太多,可以看官方说明:

(1)  微信分享sdk:  https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_cn

(2) 微信好友分享原生实现:

 public static void sharedtowx(context context,uri uri,string filepath) {
  if(sendmsg == null) return;
 intent intent = new intent(intent.action_send);
 if (!(context instanceof activity)) {
 intent.setflags(intent.flag_activity_new_task);
 }
 //intent.settype("text/plain"); //文本分享
   intent.settype("image/*");
 if(uri != null){
 intent.putextra(intent.extra_stream, uri);
 }else{
 intent.putextra(intent.extra_stream, filepath);
 }
intent.setpackage("com.tencent.mm");
  intent.setclassname("com.tencent.mm", "com.tencent.mm.ui.tools.shareimgui");//微信
  context.startactivity(intent);
 }

(3)微信朋友圈原生分享

public static void sharedtowx(context context,uri uri,string filepath) {
  if(sendmsg == null) return;
  intent intent = new intent(intent.action_send);
  if (!(context instanceof activity)) {
 intent.setflags(intent.flag_activity_new_task);
  }
  //intent.settype("text/plain"); //文本分享
 intent.settype("image/*");
  if(uri != null){
 intent.putextra(intent.extra_stream, uri);
  }else{
 intent.putextra(intent.extra_stream, filepath);
  }
  intent.setpackage("com.tencent.mm");
  intent.setclassname("com.tencent.mm", "com.tencent.mm.ui.tools.sharetotimelineui");//微信朋友圈
  context.startactivity(intent);
}

注意:微信sdk分享图文分享方式,图片+文本大小不能超过32kb,超过32kb就不能分享。

二.分享qq与qq空间

注意:qq分享支持原生分享也支持sdk分享,qq空间由于qq空间页面activity不对外开放,所以qq空间分享只支持sdk分享,不支持原生qq空间分享,而且qq空间分享早期支   持纯图片分享,但是google从android4.0开始,关闭对apn权限的自动获取,如果再使用qq分享sdk往qq空间分享纯图片就会抛出no permission to write apn setting或者分享 失败。

(1)qq分享sdk官司方文档: http://wiki.open.qq.com/wiki/%e5%88%86%e4%ba%ab%e6%b6%88%e6%81%af%e5%88%b0qq%ef%bc%88%e5%ae%9a%e5%90%91%e5%88%86%e4%ba%ab%ef%bc%89

(2)qq分享原生实现

 public static void sharedtowx(context context,uri uri,string filepath) {
  if(sendmsg == null) return;
  intent intent = new intent(intent.action_send);
  if (!(context instanceof activity)) {
 intent.setflags(intent.flag_activity_new_task);
  }
  //intent.settype("text/plain"); //文本分享
 intent.settype("image/*");
  if(uri != null){
 intent.putextra(intent.extra_stream, uri);
  }else{
 intent.putextra(intent.extra_stream, filepath);
  }
  intent.setpackage("com.tencent.mobileqq");
  intent.setclassname("com.tencent.mobileqq", "com.tencent.mobileqq.activity.jumpactivity");//qq
  context.startactivity(intent);
}

(3)利用qq分享sdk直接向qq空间分享纯图片

备注重点:要向qq空间分享纯图片,分享sdk其实是不支持纯图片分享的,但是可以使用向qq分享纯图片,设置自动打开qq空间,这样会弹出框分享纯图片,很大几    率会分享成功,图片不易过大,过大分享成功的可能性就降低了。

public static void sharetoqqzoneimg(string localimgurl){
  if(textutils.isempty(localimgurl) || sharebean == null) return;
  tencent tencent = tencent.createinstance("qq_app_id", applicatoncontext); //要向腾讯平台申请appid
  bundle bundle = new bundle();
  bundle.putint(qqshare.share_to_qq_key_type, qqshare.share_to_qq_type_image); //注意,要向qq空间分享纯图片,只能传这三个参数,不能传其他的
  bundle.putstring(qqshare.share_to_qq_image_local_url,localimgurl); //localimgurl必须是本地手机图片地址
  bundle.putint(qqshare.share_to_qq_ext_int, qqshare.share_to_qq_flag_qzone_auto_open);
  tencent.sharetoqq(activity, bundle, sharelistener);
}

综上:如果有同学对分享纯图片到qq空间有好的方法,感谢分享出来哈,笔者也是被坑了n次后,才找到这么个有一定失败率的分享方式。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接