0

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

  1. The ACTION_CREATE_DOCUMENT -> intent action allows users to save a file in a specific location.
  2. The ACTION_OPEN_DOCUMENT intent action allows users to select a specific document or file to open.
  3. 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?

James Z
  • 12,209
  • 10
  • 24
  • 44
Ritesh Adulkar
  • 841
  • 11
  • 18
  • 1
    "I want to share PDF file from my app to other apps like whatspp, gmail etc." -- use `FileProvider`. – CommonsWare Jul 28 '21 at 11:20
  • 1
    Yes i am using >>FileProvider. But Environment.getExternalStoragePublicDirectory is deprecated, instead migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore or Intent#ACTION_OPEN_DOCUMENT. So i have just tried with Context#getExternalFilesDir(String), and its working for me. Am i doing correct here? – Ritesh Adulkar Jul 28 '21 at 11:40
  • 1
    Yes! If you are sharing with `FileProvider`, the file does not even need to be on [external storage](https://commonsware.com/blog/2019/10/08/storage-situation-external-storage.html). It could be on [internal storage](https://commonsware.com/blog/2019/10/06/storage-situation-internal-storage.html), such as `Context#getFilesDir()`. – CommonsWare Jul 28 '21 at 12:37
  • yeah, so i tried with Context#getFilesDir() as well as Context#getExternalFilesDir(String) and in both the cases its working as expected but i can also see exception warning in logcat console i.e java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://my_package_name/app_terms.pdf from pid=14373, uid=1000 requires the provider be exported, or grantUriPermission(). but why its showing? – Ritesh Adulkar Jul 29 '21 at 08:03
  • Sorry, but I cannot answer that as it stands. If it concerns you, you might want to ask a separate Stack Overflow question with an updated [mcve] and the stack trace. – CommonsWare Jul 29 '21 at 10:55
  • Sure will do that and many thanks to you. You showed me the right direction :) – Ritesh Adulkar Jul 29 '21 at 12:36

0 Answers0