3
I’m using the following code:
@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();
}
}
And I’ve also stated on Android Manifest:
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE
- build Gradle version :
- compileSdkVersion 24
- buildToolsVersion '24.0.0'
- minSdkVersion 8
- targetSdkVersion 24
The program runs normally on my cell, but after generation of the pdf file I can’t find anywhere.
Has two
e.printStackTrace();
, see if anything was written on Stacktrace.– ramaral
I have already checked, but this is generated by the program itself
– Chen H. Shih
There is nothing concerning
FileNotFoundException
orDocumentException
? What did you do to be able to say "I can’t find anywhere"?– ramaral
type the app runs normally, but when I click generate pdf, and then I go in my files, in SD, and find nothing
– Chen H. Shih
was researching, is it not something related to android permission? type speaks on the site Developer.android (Caution: currently, applications can read external storage without needing special permission. However, this will be changed in future releases. If your application needs to read the external storage (but not write to it), you must declare the READ_EXTERNAL_STORAGE permission. To ensure that the application continues to function properly, declare this permission now before the changes take effect.)
– Chen H. Shih
You already have the permission there(at least until version 5.1). What is the Android version of your cel.?
– ramaral
my android is 6.01
– Chen H. Shih
has how to change getExternalStorageDirectory() to getExternalFilesDir() ?? has difference??
– Chen H. Shih
It’s a lack of permission. Look at this reply
– ramaral
Tbm to thinking it’s a lack of permission, but how do I allow?
– Chen H. Shih
Could you help me implement the code? i switch Manifest.permission.RECEIVE_SMS to Manifest.permission.WRITE_EXTERNAL_STORAGE?
– Chen H. Shih
Yes, that’s one thing to do. Note that the reply just indicates what you have to do, to implement you have to realize what each part does.
– ramaral
I’ll try here, thank you!
– Chen H. Shih