我想从我的Android应用程序生成pdf。
我已在清单文件中添加了必需的权限,并在活动中明确要求读取和写入存储权限。该应用程序已成功询问并授予了权限,但是我无法创建文件并将其存储在目录中。
这是相同的代码
private void generatePdf(){
String[] PERMISSIONS = {
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
};
if(!hasPermissions(getApplicationContext(), PERMISSIONS)){
ActivityCompat.requestPermissions(ViewNoticeActivity.this, PERMISSIONS, PERMISSION_ALL);
}else {
Log.i(TAG,"Permissions are granted");
String fname = "ilaaka_notice";
String fpath = android.os.Environment.getExternalStorageDirectory().getPath() + fname + ".pdf";
File file = new File(fpath);
if (!file.exists()) {
try {
file.createNewFile();
Log.i(TAG, "new file created");
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "An error occurred in creating file " + e.getMessage());
}
}
Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile()));
Log.i(TAG, "File created");
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "An error occurred in creating file 2 " + e.getMessage());
}
document.open();
try {
document.add(new Paragraph("My First Pdf !"));
document.add(new Paragraph("Hello World"));
Log.i(TAG, "Data added");
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL:
if(grantResults!=null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.i(TAG,"Permissions are granted");
}
else{
Log.i(TAG,"Permissions are denied");
}
break;
}
}
日志文件
/System.err: java.io.IOException: Permission denied
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.File.createNewFile(File.java:1345)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at com.replon.www.replonhomy.NoticeBoard.ViewNoticeActivity.generatePdf(ViewNoticeActivity.java:161)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at com.replon.www.replonhomy.NoticeBoard.ViewNoticeActivity.onCreate(ViewNoticeActivity.java:131)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Activity.performCreate(Activity.java:7149)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Activity.performCreate(Activity.java:7140)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2019-11-18 22:03:16.212 7912-7912/com.replon.www.replonhomy W/System.err: at android.os.Looper.loop(Looper.java:193)
2019-11-18 22:03:16.213 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6863)
2019-11-18 22:03:16.213 7912-7912/com.replon.www.replonhomy W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-11-18 22:03:16.213 7912-7912/com.replon.www.replonhomy W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
2019-11-18 22:03:16.213 7912-7912/com.replon.www.replonhomy W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-11-18 22:03:16.213 7912-7912/com.replon.www.replonhomy I/ViewNoticeActivity: An error occurred in creating file Permission denied
2019-11-18 22:03:16.226 7912-7912/com.replon.www.replonhomy W/System.err: java.io.FileNotFoundException: /storage/emulated/0ilaaka_notice.pdf (Permission denied)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.FileOutputStream.open0(Native Method)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.FileOutputStream.open(FileOutputStream.java:308)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at com.replon.www.replonhomy.NoticeBoard.ViewNoticeActivity.generatePdf(ViewNoticeActivity.java:173)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at com.replon.www.replonhomy.NoticeBoard.ViewNoticeActivity.onCreate(ViewNoticeActivity.java:131)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Activity.performCreate(Activity.java:7149)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Activity.performCreate(Activity.java:7140)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-11-18 22:03:16.227 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at android.os.Looper.loop(Looper.java:193)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6863)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-11-18 22:03:16.228 7912-7912/com.replon.www.replonhomy I/ViewNoticeActivity: An error occurred in creating file 2 /storage/emulated/0ilaaka_notice.pdf (Permission denied)
清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
任何帮助将不胜感激!预先谢谢你:)