在Android中生成PDF,无需任何第三方库

我需要使用API​​ 19中引入的PdfDocument类从我的应用程序生成PDF。我不想使用任何第三方库。
这就是我所做的

 PdfDocument document = new PdfDocument(); PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 300, 1).create(); PdfDocument.Page page = document.startPage(pageInfo); View content = findViewById(R.id.testText); content.draw(page.getCanvas()); document.finishPage(page); String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AppName"; File dir = new File(fullPath); File file = new File(fullPath, "TripReport.PDF"); if (!dir.exists()) dir.mkdirs(); if (file.exists()) file.delete(); file.createNewFile(); FileOutputStream os = new FileOutputStream(file); document.writeTo(os); document.close(); os.flush(); os.close(); 

任何人都可以建议我做错了什么?

我有简单的解决方案,它也工作………

 try{ File docfile=new File(""+getExternalFilesDir("parent dir"),"filename.pdf"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(docfile), "application/pdf"); startActivity(intent); } catch (Exception e) {}