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.