gpt4 book ai didi

java.io.FileNotFoundException :/storage/emulated/0/MyPDFDocument/questions. pdf(没有这样的文件或目录)

转载 作者:行者123 更新时间:2023-11-29 00:03:01 25 4
gpt4 key购买 nike

所以,我想要实现的是将回收站 View 内容保存在 pdf 文件中。我已经使用 PDFDocument 类 来实现这一点。我已经添加了权限。

uses-permission 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 级别 > 23 中,您必须请求 WRITE_EXTERNAL_STORAGE 的运行时权限。由于它包含在危险权限中,因此您必须提供运行时权限以允许用户明确允许这些权限。所以,仅仅在manifest中添加write external storage权限是行不通的。

以下是我用来解决问题的链接。

https://developer.android.com/training/permissions/requesting.html#java

Storage permission error in Marshmallow

关于java.io.FileNotFoundException :/storage/emulated/0/MyPDFDocument/questions. pdf(没有这样的文件或目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49672209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com