当前位置: 移动技术网 > 移动技术>移动开发>Android > Android系统图片分享工具类

Android系统图片分享工具类

2019年07月29日  | 移动技术网移动技术  | 我要评论

简介

记录一个利用系统分享功能进行图片分享的工具类(代码是用kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出)。调用方式比较简单:

util.startshareimage(this) //this为当前的activity实例

权限

记得添加文件操作权限, 另外需要注意6.0版本以上的权限管理

 <uses-permission android:name="android.permission.write_external_storage"/>
 <uses-permission android:name="android.permission.read_external_storage"/>

具体细节见代码

/**
 * 系统分享图片功能
 * created by wiky on 2018/1/13.
 */

object util {

 fun startshareimage(activity: activity) {
  //过滤出需要分享到对应的平台:微信好友、朋友圈、qq好友。 可自行修改
  val targetapp = arrayof("com.tencent.mm.ui.tools.shareimgui", "com.tencent.mm.ui.tools.sharetotimelineui", "com.tencent.mobileqq.activity.jumpactivity")
  /** * 分享图片 */
  val bitmap = getimagefromassetsfile(activity, "img_share.jpg") //从assets目录中取到对应的文件,文件名自行修改
  val localimage = savebitmap(bitmap!!, "share.jpg") //分享前,需要先将图片存在本地(记得添加权限),文件名自行修改
  val shareintent = intent(intent.action_send)
  shareintent.type = "image/*" //设置分享内容的类型:图片
  shareintent.putextra(intent.extra_stream, localimage)
  try {
   val resinfo = activity.packagemanager.queryintentactivities(shareintent, 0)
   if (!resinfo.isempty()) {
    val targetedshareintents = arraylist<intent>()
    for (info in resinfo) {
     val targeted = intent(intent.action_send)
     targeted.type = "image/*" //设置分享内容的类型
     val activityinfo = info.activityinfo
     //如果还需要分享至其它平台,可以打印出具体信息,然后找到对应的activity名称,填入上面的数组中即可
//     println("package = ${activityinfo.packagename}, activity = ${activityinfo.name}")

     //进行过滤(只显示需要分享的平台)
     if (targetapp.any { it == activityinfo.name }) {
      val comp = componentname(activityinfo.packagename, activityinfo.name)
      targeted.component = comp
      targeted.putextra(intent.extra_stream, localimage)
      targetedshareintents.add(targeted)
     }
    }
    val chooserintent = intent.createchooser(targetedshareintents.removeat(0), "选择要分享到的平台")
    if (chooserintent != null) {
     chooserintent.putextra(intent.extra_initial_intents, targetedshareintents.totypedarray<parcelable>())
     activity.startactivity(chooserintent)
    }
   }
  } catch (e: exception) {
   log.e(statconstants.log_tag, "unable to share image, logs : " + e.tostring())
  }
 }

 /** * 从assets中读取图片 */
 private fun getimagefromassetsfile(context: context, filename: string): bitmap? {
  var image: bitmap? = null
  val am = context.resources.assets
  try {
   val inputstream = am.open(filename)
   image = bitmapfactory.decodestream(inputstream)
   inputstream.close()
  } catch (e: ioexception) {
   e.printstacktrace()
  }

  return image
 }

 /** * 将图片存到本地 */
 private fun savebitmap(bm: bitmap, picname: string): uri? {
  try {
   val dir = environment.getexternalstoragedirectory().absolutepath + file.separator + picname
   val f = file(dir)
   if (!f.exists()) {
    f.parentfile.mkdirs()
    f.createnewfile()
   }
   val out = fileoutputstream(f)
   bm.compress(bitmap.compressformat.jpeg, 90, out)
   out.flush()
   out.close()
   return uri.fromfile(f)
  } catch (e: filenotfoundexception) {
   e.printstacktrace()
  } catch (e: ioexception) {
   e.printstacktrace()
  }

  return null
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网