gpt4 book ai didi

java - Android:无法读取用户使用 URI 中的 getString() 和 getPath() 选择的 PDF 文件

转载 作者:行者123 更新时间:2023-12-01 18:58:24 25 4
gpt4 key购买 nike

我正在使用 PdfBox-Android ( https://github.com/TomRoush/PdfBox-Android ) 库来读取 PDF 文件。当用户单击按钮(w/id file1)时,会弹出一个文件选择器,他或她可以在其中选择要读入的 pdf 文件。但是,当我从用户选择的 URI 中获取 URI 时,文件并使用 EITHER getString() 或 getPath() 获取文件名,我收到以下错误(使用 toString()):

Exception: java.io.FileNotFoundException: content:/com.android.providers.downloads.documents/document/7306: open failed: ENOENT (No such file or directory)

使用 getPath() 时出现以下错误:

Exception java.io.FileNotFoundException: /document/7097: open failed: ENOENT (No such file or directory)

下面是我的代码:

import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.text.PDFTextStripper
import com.tom_roush.pdfbox.util.PDFBoxResourceLoader

// File picker implementation
private fun chooseFile(view:View) {
println("chooseFile activated!");
var selectFile = Intent(Intent.ACTION_GET_CONTENT)
selectFile.type = "*/*"
selectFile = Intent.createChooser(selectFile, "Choose a file")
startActivityForResult(selectFile, READ_IN_FILE)
}


/* After startActivityForResult is executed, when the selectFile Intent is completed, onActivityResult is executed with
the result code READ_IN_FILE.*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == READ_IN_FILE) { // Step 1: When a result has been received, check if it is the result for READ_IN_FILE
if (resultCode == Activity.RESULT_OK) { // Step 2: Check if the operation to retrieve thea ctivity's result is successful
// Attempt to retrieve the file
try {
// Retrieve the true file path of the file
var uri: Uri? = data?.getData();
// Initialize and load the PDF document reader for the file the user selected
var document = PDDocument.load(File(uri?.path));

// Read in the text of the PDF document
var documentText = PDFTextStripper().getText(document);

println("documentText = " + documentText);

document.close();
} catch (e: Exception) { // If the app failed to attempt to retrieve the error file, throw an error alert
println("EXCEPTION: " + e.toString());
}
}
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Initialize the PDFBox library's resource loader
PDFBoxResourceLoader.init(applicationContext);

setContentView(R.layout.activity_main)

var file1:Button = findViewById(R.id.file1);
file1.setOnClickListener(::chooseFile)
}

最佳答案

在调用 PDFBox 之前,强烈建议初始化库的资源加载器。在调用 PDFBox 方法之前添加以下行:

PDFBoxResourceLoader.init(getApplicationContext());

所以编辑你的代码

.....
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

PDFBoxResourceLoader.init(getApplicationContext());//call this
var file1:Button = findViewById(R.id.file1);
file1.setOnClickListener(::chooseFile)
}

关于java - Android:无法读取用户使用 URI 中的 getString() 和 getPath() 选择的 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59656311/

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