1

How to Create A PDF fiel in raw ..Eventhough I Was using iText.jar I was geting an error specifying File Not Found Error and also saying File cannot be Opened .Here is the code

/** Called when the activity is first created. */

EditText editText;
Button button;
private static String FILE = "C:/FirstPdf.pdf";
String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    editText = (EditText)findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            body = editText.getText().toString();

            Document document = new Document();
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                document.open();
                document.addSubject(body);
                document.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    });

}
kishore
  • 19
  • 1
  • 8

1 Answers1

1
EditText editText;
Button button;
private static String FILE = "";
 String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FILE=(Environment.getExternalStorageDirectory().getAbsolutePath());
editText = (EditText)findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        body = editText.getText().toString();

        Document document = new Document();
        try {
             File temp = new File(FILE.getAbsolutePath(),"abcd.pdf");   
            PdfWriter.getInstance(document, new FileOutputStream(temp));
            document.open();
            document.addSubject(body);
            document.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
});

}

there is no C: in android use Environment.getExternalStorageDirectory().getAbsolutePath() to get external storage path

in manifest add permission to write external storage

Athul Harikumar
  • 2,501
  • 18
  • 16
  • I mean As there is no device available right now i tried to save that in local disk.To Check whether my code was working or not – kishore Feb 27 '13 at 09:27
  • sorry your emmulator is a total device it doesnt have any connection with your s/m local storage dear and you can still try it like this and will be stored inside emulator – Athul Harikumar Feb 27 '13 at 09:28
  • I Was facing the same problem again even after changing the code in the way you Described – kishore Feb 27 '13 at 10:15
  • FILE=(Environment.getExternalStorageDirectory().getAbsolutePath()+"abcd.pdf"); editText = (EditText)findViewById(R.id.editText1); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { – kishore Feb 27 '13 at 10:17
  • 02-27 15:42:37.379: W/System.err(575): java.io.FileNotFoundException: /mnt/sdcardabcd.pdf: open failed: EROFS (Read-only file system) – kishore Feb 27 '13 at 10:20
  • new FileOutputStream(new File(FILE)); try this – Athul Harikumar Feb 27 '13 at 10:22
  • and please make sure you add the permission to write external storage directory in manifest – Athul Harikumar Feb 27 '13 at 10:25
  • 02-27 15:55:05.841: W/System.err(631): java.io.FileNotFoundException: /mnt/sdcardabcd.pdf: open failed: EROFS (Read-only file system) – kishore Feb 27 '13 at 10:26
  • have you used the latest code as your error shows /mnt/sdcardabcd.pdf: where as it should be /mnt/sdcard/abcd.pdf File temp = new File(FILE.getAbsolutePath(),"abcd.pdf"); PdfWriter.getInstance(document, new FileOutputStream(temp)); – Athul Harikumar Feb 27 '13 at 10:29
  • 02-27 16:04:55.559: W/System.err(677): java.io.FileNotFoundException: /mnt/sdcard/abcd.pdf: open failed: EACCES (Permission denied) – kishore Feb 27 '13 at 10:35
  • if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"a"); else cacheDir=context.getCacheDir(); if(!cacheDir.exists()) cacheDir.mkdirs(); and pass this dir instead of file .. this is the worst case but probably as you are using an emulator the error u are getting is a permission denied – Athul Harikumar Feb 27 '13 at 10:48
  • no i hope not i am able to create files without any problem in emulator i hope you are messing up something else – Athul Harikumar Feb 27 '13 at 10:55
  • But everything is clear i had a editText and button in my main.xml and had iText in libs and configured that and made modifications as you said... – kishore Feb 27 '13 at 11:01
  • for the last try splitting FileOutputStream aaa=new FileOutputStream(temp); PdfWriter.getInstance(document, aaa); and run in debugging mode and check what each returns – Athul Harikumar Feb 27 '13 at 11:03
  • No use I Think I have to test this in Device – kishore Feb 27 '13 at 11:32
  • i tested tis code in an emulator and the file was created succesfully except for the itext jars some class missing (hope i am note using the right jar) i dont know about the particular jar in detail but abcd.pdf was created successfully even though it had no contents the warning was abcd.pdf contains no pages – Athul Harikumar Feb 27 '13 at 11:59