android文档标题中的日期?(Date in android document title?)

编程入门 行业动态 更新时间:2024-10-28 18:34:58
android文档标题中的日期?(Date in android document title?)

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

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生成正确,但我需要日期在标题中。 我能做什么? :(

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? :(

最满意答案

问题似乎是因为: 。 请参阅Android上文件名中允许的字符数? 。 :是一种保留字符,所以不能在文件名中使用。 尝试使用.replace函数替换为"" (即空字符串)。 然后用它来创建PDF。 另见Android - 如何用另一个字符串替换部分字符串?

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?

更多推荐

PDF,new,dateTime,date,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-07-06 20:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1054536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   文档   标题   android   document

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!