如何在Android中实现分享图片功能

发布时间:2021-05-22 16:47:07 作者:Leah
来源:亿速云 阅读:570

本篇文章为大家展示了如何在Android中实现分享图片功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

调用方式:

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
  }

}

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

上述内容就是如何在Android中实现分享图片功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如使用JavaScript实现无刷新上传预览图片功能
  2. 怎么在iOS中实现微信分享多张图片功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:怎么在Android中通过自定义控件实现时钟功能

下一篇:使用Spring怎么实现请求参数校验功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》