Android系统图片分享工具类

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android系统图片分享工具类脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

简介

记录一个利用系统分享功能进行图片分享的工具类(代码是用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&#63; {   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  }  }
android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android系统图片分享工具类全部内容,希望文章能够帮你解决Android系统图片分享工具类所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。