-1

I am using this code to generate pdf file in android but it gives me "File not found exception"

            try {
    OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

    Document document = new Document();
 //   PdfWriter.getInstance(document, new FileOutputStream(FILE));
    PdfWriter.getInstance(document, file);
    document.open();
    addMetaData(document);
    addTitlePage(document);
    addContent(document);
    //createImage();
    document.close();

} catch (Exception e) {
    e.printStackTrace();
}

when I execute this line:

    PdfWriter.getInstance(document, file);

It says "Java.io.FileNotFOundException". I have to create new file then why is it open a file which is not even generated yet? what is wrong with this code?

Paras
  • 3,197
  • 2
  • 20
  • 30
just a learner
  • 57
  • 1
  • 11

2 Answers2

3

I dont think "D:\" is a valid file location in android

try

OutputStream file = new FileOutputStream(newFile(Environment.getExternalStorageDirectory().toString,"test.pdf"));

As a bit of extra info for in future if you are dealing with file systems in android, becasue android is unix based the path separator is '/' not '\'. The '\' separator is (as far as I know) something unique to windows as is D:

Cob50nm
  • 911
  • 2
  • 9
  • 27
  • I have found this code under "how to generate pdf files in android" – just a learner Sep 03 '13 at 12:35
  • I my google search of "how to generate pdf files in android" didn't bring me to any code like you mention.The "\" is the problem, it is not a valid path separator in android. Have you tried my answer? – Cob50nm Sep 03 '13 at 12:41
  • Cob50nm has the reason, if you want to work with files, should use the getExternalStorageDirectory() property of Environment. Too you can try with `OutputStream file = new FileOutputStream(new File("/mnt/sdcard/test.pdf"));` but this is inadvisable, cause not in all devices /mnt/sdcard is the sdcard, for someones is /mnt/sdcard0 or /mnt/extsdacrd... So you should use the answer given by Cob50nm – Leonardo Sapuy Sep 03 '13 at 12:42
  • I see the error now and have changed the answer, I forgot to make `test.pdf` a string – Cob50nm Sep 03 '13 at 12:44
  • then your device doesn't have a public storage directory. Are you working on an emulator or a real device? Some of the lower range android phones do not have internal storage and require an sdcard, the samsung Galaxy y, is an example of this. – Cob50nm Sep 03 '13 at 12:50
  • Its working now..i didn't add the permissions in menifest file. NOw i added permission and used your path. Thankyou – just a learner Sep 03 '13 at 12:51
0

This error is because of the location provided by you in your line of code mentioned below.


    OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

I don't think D drive exist for your android file system. Below link might help.

How To Create a PDF file in raw in Android

or you can use below code.


    try {
                 File temp = new File(FILE.getAbsolutePath(),"Test.pdf");   
                  PdfWriter.getInstance(document, new FileOutputStream(temp));
    }

Community
  • 1
  • 1
Paras
  • 3,197
  • 2
  • 20
  • 30