【问题标题】:Date in android document title?android文档标题中的日期?
【发布时间】:2013-09-02 03:07:09
【问题描述】:

好吧,我在 Android 应用程序中生成了一个 PDF,但我需要标题才能包含系统的日期和时间。所以我编译了这段代码:

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);

但是当我尝试在应用程序中生成 PDF 时,日志给了我这个错误:

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)

当我删除“dateTime”变量时,PDF 会正确生成,但我需要将日期放在标题中。我能做些什么? :(

【问题讨论】:

  • 错误似乎是 FileNotFound。您确定文件名在其名称中包含日期和时间......并且格式和顺序相同? Ty 打印您想要使用的名称,并在 cpmpae 后面加上 tile 的实际名称。
  • 可能是 dateTime 变量有一个特殊的字符,不允许将文件命名为 /
  • 这可能是系统给我日期的方式。有没有其他方法可以得到没有禁止字符的日期?
  • 现在的格式是“Mm dd, yyyy hr:min:sec am”

标签: android eclipse date pdf


【解决方案1】:

问题似乎是因为:。见What characters allowed in file names on Android?: 是一种保留字符,因此不能用于文件名。尝试使用.replace 函数将其替换为""(即空字符串)。然后用它来创建PDF。另见Android - how to replace part of a string by another string?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多