Java Android:无法从URI中同时使用getString()和getPath()读取用户选择的PDF文件
我正在使用PdfBox Android()库读取PDF文件。当用户单击按钮(w/id file1)时,会弹出一个文件选择器,用户可以在其中选择要读取的pdf文件。但是,当我从用户选择的文件中获取URI并使用getString()或getPath()获取文件名时,我收到以下错误(使用Java Android:无法从URI中同时使用getString()和getPath()读取用户选择的PDF文件,java,android,file,kotlin,io,Java,Android,File,Kotlin,Io,我正在使用PdfBox Android()库读取PDF文件。当用户单击按钮(w/id file1)时,会弹出一个文件选择器,用户可以在其中选择要读取的pdf文件。但是,当我从用户选择的文件中获取URI并使用getString()或getPath()获取文件名时,我收到以下错误(使用toString()): 异常:java.io.FileNotFoundException: 内容:/com.android.providers.downloads.documents/document/7306:打开
toString()
):
异常:java.io.FileNotFoundException:
内容:/com.android.providers.downloads.documents/document/7306:打开
失败:enoint(没有这样的文件或目录)
使用getPath()
时出现以下错误:
异常java.io.FileNotFoundException:/document/7097:打开失败:enoint(否
此类文件或目录)
下面是我的代码:
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)
}
在调用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)
}
不幸的是,这并没有解决问题problem@AdamLee你找到解决办法了吗?我也有同样的问题,这个解决方案也帮不了我。不幸的是,这并没有解决问题problem@AdamLee你找到解决办法了吗?我也有同样的问题,而且该解决方案也不能帮助我
PDDocument.load(File(uri?.path))
不要尝试从内容方案的uri生成文件。类似于PDDocument.load(uri)
的东西应该是可能的。如果没有,请他们实现它。在Android中有没有办法将URI加载到流中?如果是,则传递该流。PDDocument.load(File(uri?.path))
不要尝试从内容方案的uri生成文件。类似于PDDocument.load(uri)
的东西应该是可能的。如果没有,请他们实现它。在Android中有没有办法将URI加载到流中?如果是,则通过该流。