无法在仿真器SDCard中写入PDF

时间:2013-06-02 09:07:58

标签: android itext

我正在使用itext库,我无法在模拟器SD卡中编写pdf,即使我从我的角度来看代码似乎是我无法找到的问题,是的,我还添加了写入外部存储权限。它给我发现文件未找到例外,

try
 {

                    String path = Environment.getExternalStorageDirectory()+"/Hello/";
                        File file = new File(path+"hello.pdf"); 
                        System.out.println(file.toString());
                        if(!file.exists()){
                            file.getParentFile().mkdirs();
                            try { 
                                file.createNewFile(); 

                            }
                            catch (IOException e) 
                            { 
                                // TODO Auto-generated catch block e.printStackTrace(); } 
                            }
                        }
                        Document document = new Document();
                        PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()
                                 +File.separator
                                 +"Hello" //folder name
                                 +File.separator
                                 +"hello.pdf"));
                        document.open();
                        document.add(new Paragraph("hello"));
                            document.close();

1 个答案:

答案 0 :(得分:1)

File path = new File (Environment.getExternalStorageDirectory(),"Hello");
if (!path.exists()) {
   path.mkdir();
}

File file = new File(path, "hello.pdf"); 
System.out.println(file.toString());
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file);
document.open();
document.add(new Paragraph("hello");
document.close();
相关问题