![](/img/trans.png)
[英]Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory)
[英]java.io.FileNotFoundException: /storage/emulated/0/MyPDFDocument/questions.pdf (No such file or directory)
所以,我想要实现的是将回收器视图内容保存在 pdf 文件中。 我已经使用PDFDocument 类来实现这一点。 我已经添加了权限。
使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE"
我不知道我在这里做错了什么。 我曾尝试更改存储位置,但每次都出现相同的错误。
如果有人能告诉我我在这里做错了什么。 下面是相关代码:
if (selectedId == R.id.action_save_pdf) {
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo =
new PdfDocument.PageInfo.Builder(100, 100, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
// finish the page
document.finishPage(page);
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/MyPDFDocument");
if (!dir.exists()) {
dir.mkdir();
}
File filePath = new File(dir, "questions.pdf");
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Saved to PDF", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Unsuccessful!",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "External Storage not available!", Toast.LENGTH_SHORT).show();
}
// close the document
document.close();
return true;
}
return super.onOptionsItemSelected(item);
}
提前谢谢你。
我找到了我自己的答案。 实际上在 android api level > 23 中,您必须为 WRITE_EXTERNAL_STORAGE 请求运行时权限。 由于它包含在危险权限中,您必须提供运行时权限以允许用户明确允许这些权限。 因此,仅在清单中添加写入外部存储权限是行不通的。
以下是我用来解决问题的链接。
https://developer.android.com/training/permissions/requesting.html#java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.