0

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();
        }
    }
  • 1
    You need to [request `WRITE_EXTERNAL_STORAGE` at runtime](https://developer.android.com/training/permissions/requesting). You need to [stop hard-coding paths](https://commonsware.com/blog/2019/10/08/storage-situation-external-storage.html). You need to be aware that [your hard-coded location is not available by default on Android 10 and higher](https://stackoverflow.com/q/64119230/115145). And, depending on how you are looking for the file, you need to [have `MediaStore` index it](https://stackoverflow.com/a/32789206/115145). – CommonsWare Nov 21 '20 at 01:03
  • `String path = "/sdcard/" + "ArquivoAndroid.pdf";` Cange to `File file = new File(getExternalFilesDir(null), "ArquivoAndroid.pdf");` – blackapps Nov 21 '20 at 08:21
  • `File file = new File(path); if (!file.exists()) { file.createNewFile(); }` Remove those lines. No permissions needed. – blackapps Nov 21 '20 at 08:22
  • https://medium.com/android-school/exploring-itext-to-create-pdf-in-android-5577881998c8 – Pratik Butani Feb 19 '21 at 07:50

0 Answers0