【问题标题】:Is it possible to share a file that stored using storage access framework with other apps是否可以与其他应用程序共享使用存储访问框架存储的文件
【发布时间】:2021-05-05 16:11:34
【问题描述】:

我使用存储访问框架将文档保存到磁盘。有没有办法使用 Intent 与其他应用共享该特定文档文件?

我使用以下方法分享:

    fun Activity.sharePdf(pdfUri: Uri) {

    var intent = Intent(Intent.ACTION_SEND)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        val uri =
            FileProvider.getUriForFile(
                this,
                this.packageName.toString() + ".provider",
                File(pdfUri.toString())
            )
        intent.type = "application/pdf";
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
    } else {
        intent.type = "application/pdf";
        intent.putExtra(Intent.EXTRA_STREAM, pdfUri.toString())
        intent = Intent.createChooser(intent, "Open File")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    startActivity(intent)
}

这不适用于使用 SAF 存储的文件。

【问题讨论】:

    标签: storage-access-framework scoped-storage


    【解决方案1】:

    这对我有用

    fun Activity.sharePdf(pdfUri: Uri) {
    
    val intent = Intent(Intent.ACTION_SEND)
    intent.type = "application/pdf"
    intent.putExtra(Intent.EXTRA_STREAM, pdfUri)
    intent.clipData = ClipData("fileName", arrayOf("application/pdf"), ClipData.Item(pdfUri))
       
    
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        startActivity(Intent.createChooser(intent, "chooserTitle"))
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-14
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多