4

How to save pdf files in default Documents folder in external storage of the android?

I have tried with:

       Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
       intent.addCategory(Intent.CATEGORY_OPENABLE);
       intent.setType("application/pdf");
       intent.putExtra(Intent.EXTRA_TITLE, name);
       startActivityForResult(intent,PDF_REQUEST_CODE);

But this opens another Document provider activity with save option. I want that the pdf file saves directly into Document folder from a single click on my application.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Coder
  • 129
  • 7
  • You want download pdf file ? – chand mohd Aug 25 '20 at 10:25
  • @chandmohd I have a PDFDocument type variable with PDF in it , which I want to save as PDF in Documents. Basically I want to save a variable as PDF in Documents folder. – Coder Aug 25 '20 at 10:36
  • did u check this https://stackoverflow.com/questions/34296149/creating-a-pdf-file-in-android-programmatically-and-writing-in-it – Abdulkarim Aug 25 '20 at 10:55
  • Did you try writing there directly using filesystem APIs? The documentation sucks, but [you can write to some locations directly on Android 11](https://issuetracker.google.com/issues/154750665#comment8). If that does not work, though, then you will either need to [try `MediaStore`](https://commonsware.com/blog/2020/02/15/scoped-storage-stories-undocumented-documents.html) or settle for the approach from your question. – CommonsWare Aug 25 '20 at 11:12
  • @abed That post is 4 years ago and all of its solution uses getExternalStorageDirectory which is now deprecated. Any other solution? – Coder Aug 25 '20 at 13:01
  • @CommonsWare Your answer seems to be good. I am testing it and will response here if it works. – Coder Aug 25 '20 at 13:05

0 Answers0