java—我正在尝试在android项目中生成一个.pdf文件

3zwjbxry  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(219)

在那里!
我正在尝试用android生成一个.pdf文件。因此,应用程序会打开,但不会生成文件。我试图更改路径,并在androidmanifest.xml中设置权限,但两种方法都不起作用。你能帮我吗?
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>

主活动.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();
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题