【发布时间】:2012-02-11 16:49:08
【问题描述】:
请在下面找到我的代码。我需要获取用户从 SDcard 中选择的 pdf 文档的文件路径。问题是 URI.getPath() 返回:
/file:///mnt/sdcard/my%20Report.pdf/my Report.pdf
正确的路径是:
/sdcard/my Report.pdf
请注意我在stackoverflow上搜索但找到了获取图像或视频文件路径的示例,没有如何获取PDF的文件路径示例?
我的代码,不是所有代码,只有 pdf 部分:
public void openPDF(View v)
{
Intent intent = new Intent();
//intent.setType("pdf/*");
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), SELECT_PDF_DIALOG);
}
public void onActivityResult(int requestCode, int resultCode, Intent result)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PDF_DIALOG)
{
Uri data = result.getData();
if(data.getLastPathSegment().endsWith("pdf"))
{
String pdfPath = data.getPath();
}
else
{
CommonMethods.ShowMessageBox(CraneTrackActivity.this, "Invalid file type");
}
}
}
}
能否请教我如何从 URI 中获取正确的路径?
【问题讨论】:
-
这里是问题的正确答案[看看][1][1]:stackoverflow.com/questions/3401579/…