我有以下代码,允许用户从那里设备中选择一个pdf文件,但是目前看来传递的URI路径并不是绝对的。
public void openPDF(int type){
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select PDF "), type);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PDF_CV)
{
System.out.println("SELECT_PDF");
Uri selectedImageUri = data.getData();
cvselectedPath = selectedImageUri.getPath();
System.out.println("SELECT_AUDIO Path : " + cvselectedPath);
}
}
}
作为示例,当我从模拟器cvselectedPath == /document/3
当选择的文件实际上是/sdcard/downloads/sample.pdf
时如何获取文件的实际绝对路径,以便我可以创建一个准备好处理的File
对象。
评论中给出的代码不起作用,因为我得到了一个内容:uri with it so
Uri uri = data.getData();
String type = data.getType();
Log.i(TAG,"Pick completed: "+ uri + " "+type);
if (uri != null)
{
String path = uri.toString();
if (path.toLowerCase().startsWith("file://"))
{
// Selected file/directory path is below
path = (new File(URI.create(path))).getAbsolutePath();
}
Log.i(TAG,"path completed: " + path);
}
给出
选择已完成:content:// media / external / file / 2007 application / pdf 路径已完成:content:// media / external / file / 2007