kotlin 解决微信分享图片提示“必须为图片格式”

//解决微信分享提示(**分享失败,分享多文件必须为图片格式**)问题
    fun shareFiles(activity: Activity, list: List<String>) {
        val uris = java.util.ArrayList<Uri>()
        var type: String? = null
        type = if (list[0].endsWith(".pdf")) {
            "application/pdti"
        } else if (list[0].endsWith(".xlsx") || list[0].endsWith(".xls")) {
            "application/vnd.ms-excel"
        } else if (list[0].endsWith(".doc") || list[0].endsWith(".docx")) {
            "application/msword"
        } else if (list[0].endsWith(".pptx") || list[0].endsWith(".ppt")) {
            "application/vnd.ms-powerpoint"
        } else if (list[0].endsWith(".jpg") || list[0].endsWith(".png")) {
            "image/*"
        } else if (list[0].endsWith(".txt")) {
            "text/plain"
        } else if (list[0].endsWith(".zip")) {
            "application/zip"
        } else {
            "* / *"
        }
        for (i in list.indices) {
            uris.add(Uri.fromFile(File(list[i])))
        }
        val multiple = uris.size > 1
        val intent = Intent(if (multiple) Intent.ACTION_SEND_MULTIPLE else Intent.ACTION_SEND)
        if (multiple) {
            intent.type = type
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
        } else {
            intent.type = type
            intent.putExtra(Intent.EXTRA_STREAM, uris[0])
        }
        sharedByApp(activity, 0, intent, 0)
    }


    private fun sharedByApp(context: Context, type: Int, intent: Intent, actResultcode: Int) {
        try {
            context.startActivity(
                Intent.createChooser(
                    intent,
                    context.resources.getString(R.string.share)
                )
            )
        } catch (e: Exception) {
        }
    }

文章仅用于学习笔记

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值