我尝试从android/data/mypackage/files/file.pdf分享一个pdf文件。我还在此应用程序中生成这些pdf文件,但当我尝试分享时,pdf文件不会出现在电子邮件附件的已附加文件中,或者谷歌云存储会显示类似于“无数据可共享”的消息。
下面是我的分享pdf代码:
val aName = intent.getStringExtra("iName")
val file = File(this.getExternalFilesDir(null)?.absolutePath.toString(), "$aName")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_STREAM, file)
shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
shareIntent.type = "application/pdf"
startActivity(Intent.createChooser(shareIntent, "share.."))
Toast.makeText(this,"$file",Toast.LENGTH_SHORT).show()
FileUriExposedException
异常。请使用FileProvider
。 - CommonsWareFileProvider
应该能够在 Android 15 或更早的版本上正常工作。但是,对于可见于外部存储的文件,您也可以使用Uri.forFile()
方法,这样也可能起作用。 - CommonsWare