在android中创建PDF [英] Creating PDFs in android
问题描述
我正在使用此代码在android中生成pdf文件,但它给了我找不到文件异常
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();
}
:
PdfWriter.getInstance(document, file);
它表示Java.io.FileNotFOundException。我必须创建新文件然后为什么打开一个甚至没有生成的文件?这段代码出了什么问题?
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?
推荐答案
我不认为D:\是android中的有效文件位置
I dont think "D:\" is a valid file location in android
尝试
OutputStream file = new FileOutputStream(newFile(Environment.getExternalStorageDirectory()。toString ,test.pdf));
如果你正在处理android中的文件系统,将来会有一些额外的信息,因为android是基于unix的路径分隔符是'/'而不是'\'。 '\'分隔符(据我所知)是Windows独有的东西,因为 D:
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:
这篇关于在android中创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!