I want to share PDF file from my app to other apps like whatspp, gmail etc. Scoped storage introduced from Android 11. I am getting the uri from Environment.getExternalStoragePublicDirectory
but its deprecated instead we should use Scoped storage otherwise from Android 12 it will return nothing.
val file = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath,
fileName
)
val uri = Uri.parse(pdfUrl)
val share = Intent()
share.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
share.action = Intent.ACTION_SEND
share.type = "application/pdf"
share.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(share, "Share"))
As per Android documentation we have three intent
- The
ACTION_CREATE_DOCUMENT
-> intent action allows users to save a file in a specific location. - The
ACTION_OPEN_DOCUMENT
intent action allows users to select a specific document or file to open. - The
ACTION_OPEN_DOCUMENT_TREE
intent action, allows users to select a specific directory, granting your app access to all of the files and sub-directories within that directory.
but above intent action doesn't work in my case. I want to show different share options to user, so that user can share file with other apps.
What should i do?