Java I';我正试图在Android项目中生成一个.pdf文件
那里 我正在尝试用Android生成一个.pdf文件。因此,应用程序会打开,但不会生成文件。我试图更改路径,并在AndroidManifest.xml中添加权限,但两者都不起作用。你能帮我吗 AndroidManifest.xmlJava I';我正试图在Android项目中生成一个.pdf文件,java,android,itext,Java,Android,Itext,那里 我正在尝试用Android生成一个.pdf文件。因此,应用程序会打开,但不会生成文件。我试图更改路径,并在AndroidManifest.xml中添加权限,但两者都不起作用。你能帮我吗 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/a
<?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>
你需要这样做。你需要这样做。你需要意识到这一点。而且,根据您查找文件的方式,您需要。String path=“/sdcard/”+“ArquivoAndroid.pdf”
Cange toFile File File=new文件(getExternalFilesDir(null),“ArquivoAndroid.pdf”)代码>文件文件=新文件(路径);如果(!file.exists()){file.createNewFile();}
删除这些行。不需要权限。
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();
}
}