@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdftest);
Button Btgerar = (Button) findViewById(R.id.gerarpdf);
Btgerar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CriarPDF();
}
});
}
public void CriarPDF() {
String FILE = Environment.getExternalStorageDirectory().toString() + "/PDF/" + "Ficha.pdf";
EditText txt = (EditText) findViewById(R.id.editText);
// Create New Blank Document
Document document = new Document(PageSize.A4);
// Create Directory in External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
myDir.mkdirs();
// Create Pdf Writer for Writting into New Created Document
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
// Open Document for Writting into document
document.open();
document.add(new Paragraph(txt.getText().toString()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
// Close Document after writting all content
document.close();
Toast.makeText(this, "PDF File is Created. Location : " + FILE,
Toast.LENGTH_LONG).show();
// Create new Page in PDF
document.newPage();
}
}
我已經在Android清單如何在內存中創建PDF文件
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE
,並建立gradle這個版本的聲明是:
compileSdkVersion 24
buildToolsVersion '24.0.0'
minSdkVersion 8
targetSdkVersion 24
應用程序可以正常運行,但可以」 t創建PDF文件,我找不到。