2

So, what I am trying to achieve is to save the recycler view contents in pdf file. I have used PDFDocument class to achieve that. I have already added the permission.

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

I don't know what am I doing wrong here. I have tried changing the location of the storage but everytime the same error.

If anybody can tell me what am I doing wrong here. Here is the related code:

  if (selectedId == R.id.action_save_pdf) {
        // create a new document
        PdfDocument document = new PdfDocument();

        // crate a page description
        PdfDocument.PageInfo pageInfo =
                new PdfDocument.PageInfo.Builder(100, 100, 1).create();

        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);

        // finish the page
        document.finishPage(page);

        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File root = Environment.getExternalStorageDirectory();
            File dir = new File(root.getAbsolutePath() + "/MyPDFDocument");
            if (!dir.exists()) {
                dir.mkdir();
            }
            File filePath = new File(dir, "questions.pdf");

            try {
                document.writeTo(new FileOutputStream(filePath));
                Toast.makeText(this, "Saved to PDF", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "Save Unsuccessful!",
                        Toast.LENGTH_LONG).show();
            }

        } else {
            Toast.makeText(this, "External Storage not available!", Toast.LENGTH_SHORT).show();
        }

        // close the document
        document.close();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Thank You well in advance.

Ron Daulagupu
  • 423
  • 6
  • 18
  • I thought this line `File filePath = new File(dir, "questions.pdf");` creates the necessary file. – Ron Daulagupu Apr 05 '18 at 12:55
  • @sunilsunny I tried doing what you said. Still the same error – Ron Daulagupu Apr 05 '18 at 13:03
  • `if (!dir.exists()) { dir.mkdir(); }` That should be: `if (!dir.exists()) { if ( !dir.mkdir()) {Toast(... could not make dir ....); return;}; }`. I bed the directory is not created. – greenapps Apr 05 '18 at 13:37
  • @sunil sunny `There should be something like filePath.createNewFile(); then only that file will be created. – sunil sunny 56 mins ago`. Nonsense. `new FileOutputStream(filePath)` will create the file. Well if the directory exists and permissions granted. – greenapps Apr 05 '18 at 13:39
  • `thought this line File filePath = new File(dir, "questions.pdf"); creates the necessary file. ` No. It will not. But it is not needed either. – greenapps Apr 05 '18 at 13:42
  • @greenapps I tried changing that and yes now I know that the directory is not created but why is it not creating I thought the command dir.mkdir() does that. And also I have added the required permission as you can see I have posted above. So, what am I missing? – Ron Daulagupu Apr 05 '18 at 14:08
  • `Thank You well in advance.` Well i cannot tell you as i like to do such before the thanks. – greenapps Apr 05 '18 at 14:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168320/discussion-between-ron-daulagupu-and-greenapps). – Ron Daulagupu Apr 05 '18 at 14:20

1 Answers1

3

i found my own answer. Actually in android api level > 23, you have to request runtime permission for WRITE_EXTERNAL_STORAGE. Since it is included in dangerous permissions, you have to provide runtime permissions to allow the user to explicitly allow those permissions. So, just adding write external storage permission in manifest won't work.

Below are the links I used to solve my problem.

https://developer.android.com/training/permissions/requesting.html#java

Storage permission error in Marshmallow

Ron Daulagupu
  • 423
  • 6
  • 18