there!
I'm trying to generate a .pdf file in Android. So, the app opens, but it doesn't generate the file. I tried to change the path, and to put a permission in the AndroidManifest.xml, but neither works. Can you help me with this?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.pdf">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Pdf">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Document document;
public void criandoPdf(View v) {
try {
String path = "/sdcard/" + "ArquivoAndroid.pdf";
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile()));
document.open();
document.add(new Paragraph("Aqui está meu documento pdf " + " gerado pelo Android."));
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
document.close();
Toast.makeText(this, "Documento Criado com Sucesso!", Toast.LENGTH_LONG).show();
}
}