0

I am going to create one .pdf file and I am getting file not found exception. I have written WRITE_EXTERNAL_STORAGE permission for that in AndroidManifest.xml file and also provided runtime permission for Marshmallow device.

Below is my code:

 File SDCardRoot = Environment.getExternalStorageDirectory();
 //create a new file, to save the downloaded file
 File file = new File(SDCardRoot + "xyz/", pdfFilename);
 if (!file.exists()) {
     file.mkdir();
 }
 OutputStream fileOutput = new FileOutputStream(file);
 docWriter = PdfWriter.getInstance(doc, fileOutput);

Can you please get me out?

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Jalpesh Khakhi
  • 288
  • 4
  • 17
  • Yes, that was missing but I corrected it. and it was still not working :-( – Jalpesh Khakhi Mar 06 '17 at 16:16
  • http://stackoverflow.com/questions/1239026/how-to-create-a-file-in-android – Saman Mar 06 '17 at 16:21
  • `file.mkdir();`. Check the return value as it might fail to create the directory in which case it returns false. If so then didplay a toast to the user saying so and return. Dont continue with the code then as it makes little sense trying to create a file in a directory that does not exist. – greenapps Mar 06 '17 at 16:45
  • mkdir will create a directory with the pdf filename as folder. You should first create the xyz directory and then set up a File object using the file name. – greenapps Mar 06 '17 at 16:48
  • I got the solution this link http://stackoverflow.com/a/8955442/4795817 helps me to create pdf file in sdcard – Jalpesh Khakhi Mar 07 '17 at 16:01
  • Thank you all of you for your efforts and all your suggestions, guidelines and help. Thanks – Jalpesh Khakhi Mar 07 '17 at 16:01

0 Answers0