0

Well, Im having a PDF generated in and android application, but I need the title to have the date and time of the system. So I compiled this code:

public void createPDF()
{
    Document doc = new Document();

     try {
         Date date = new Date();
         String dateTime = DateFormat.getDateTimeInstance().format(date);
         File sdCard = Environment.getExternalStorageDirectory();
         File dir = new File (sdCard.getAbsolutePath() + "/Bitacora");
         dir.mkdirs();
         File file = new File(dir, "Bitácora "+idetotrocliente.getText().toString()+", "+dateTime+vwfecha1.getText().toString()+etsitio.getText().toString()+".pdf");
         FileOutputStream fOut = new FileOutputStream(file);

But when I try to generate the PDF in the app, the log gives me this error:

08-28 21:32:38.950: E/PDFCreator(12425): ioException:java.io.FileNotFoundException: /mnt/sdcard/Bitacora/Bitácora ¡VALOR NECESARIO!, ago 28, 2013 9:32:24 p.m..pdf (Invalid argument)

And when I delete the "dateTime" variable, the PDF generates correctly, but I need the date to be in the title. What can I do? :(

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Miguel
  • 115
  • 2
  • 3
  • 14
  • Errors seems to be of FileNotFound. Are you sure that the file name contains the date and time in its name... and in same format and order ? Ty to print the name that you are rying to use and hen cpmpae with the actual name of tile. – Shobhit Puri Aug 29 '13 at 15:30
  • may be the dateTime variable has a special char not allowed to name a file like / – moh.sukhni Aug 29 '13 at 15:31
  • IT may be how the sistem gives me the date. Is there any other way to get a date without forbidden characters? – Miguel Aug 29 '13 at 15:34
  • The format now is "Mm dd, yyyy hr:min:sec am" – Miguel Aug 29 '13 at 15:35

1 Answers1

0

The problem seems to be because of :. See What characters allowed in file names on Android? . : is sort of reserved char, so can't be used in filename. Try to replace that with ""( i.e empty string) using .replace function. Then use it to create PDF. Also see Android - how to replace part of a string by another string?

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124