Android 分享图片和文案

一、业务描述

大多数App的需求中,有分享功能,点击唤起系统的分享栏,然后分享内容,例如指定文案,链接,图片等等。

二、实现逻辑

1.拿到要分享图片和内容

2.图片拿到缓存里,用于分享

3.使用FileProvider:用于创建一个 Content URI 并赋予临时的文件访问权限来代替 File URI 实现文件共享。

4.使用Intent实现分享,在intent里put Extra()图片和文本。

5.为intent添加Flag:shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

如果设置,此Intent的收件人将被授予对Intent数据中的URI及其ClipData中指定的任何URI执行读取操作的权限

三、实现代码:

根据控件临时生成一个图片进行分享,具体的逻辑请看实现代码,代码中有注释

        mBinding.shareRl.setOnClickListener {
            val width = mBinding.allContentCL.width
            val height = mBinding.allContentCL.height
            val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)
            mBinding.allContentCL.draw(canvas)
            val imageFile = File(
                cacheDir, "image.png"
            )//从父抽象路径名和子路径名字符串创建新的文件实例。就是在系统的缓存目录里创建这个文件
            try {
                val fos = FileOutputStream(imageFile)//创建一个文件输出流,以写入由指定的file对象表示的文件。将创建一个新的FileDescriptor对象来表示此文件连接。
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)//将位图的压缩版本写入指定的输出流
                fos.close()
                val authority = "$packageName.fileprovider"
                val shareIntent = Intent()
                shareIntent.action = Intent.ACTION_SEND
                shareIntent.putExtra(Intent.EXTRA_TEXT, mBinding.cardDetailContent.text)
                shareIntent.putExtra(
                    Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, authority, imageFile)
                )
                shareIntent.type = "image/*"
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                startActivity(
                    Intent.createChooser(
                        shareIntent, "Share Image"
                    )
                )
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

tips:  代码val authority = "$packageName.fileprovider"

1.需要你在manifest文件中,注册你的FileProvider,

2.the.shy.world.top.one.myapplication替换为你的包路径。

 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="the.shy.world.top.one.myapplication.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

3.file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path
        name="shared_images"
        path="." />
</paths>

 

如果想了解FileProvider和存储的特性,可以看下面的博客。

【精选】Android FileProvider特性与Intent重定向漏洞-CSDN博客

以及intent.createChooser()的使用

Intent.createChooser()-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&岁月不待人&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值