如何从URI获取文件路径?
请在下面找到我的代码。我需要获取用户从SD卡中选择的pdf文档的文件路径。问题是 URI.getPath() 返回:
/file:///mnt/sdcard/my%20Report.pdf/my Report.pdf
正确的路径是:
/sdcard/my Report.pdf
请注意,我在 stackoverflow 上搜索,但找到了获取图像或视频的 filePath 的示例,没有示例说明如何获取 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 获取正确的路径吗?
Please find my code below. I need to get the file path of the pdf document, selected by the user from SDcard. The issue is that the URI.getPath() returns:
/file:///mnt/sdcard/my%20Report.pdf/my Report.pdf
The correct path is:
/sdcard/my Report.pdf
Please note that i searched on stackoverflow but found the example of getting the filePath of image or video, there is no example of how to get the filepath in case of PDF?
My code , NOT all the code but only the pdf part:
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");
}
}
}
}
Can some please help me how to get the correct path from URI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该返回正确的路径
编辑
正如@Tron建议的工作代码是
should return u the correct path
EDIT
As @Tron suggested the working code is
这是问题的答案
这里
实际上我们必须从可共享的ContentProvider获取它相机应用程序。
编辑 。复制对我有用的答案
Here is the answer to the question
here
Actually we have to get it from the sharable ContentProvider of Camera Application.
EDIT . Copying answer that worked for me