如何从URL中的文件路径? [英] How to get the file path from URI?
问题描述
请在下面找到我的code。我需要获得PDF文档的文件路径,从SD卡的用户选择。问题是,URI.getPath()返回:
/file:///mnt/sdcard/my%20Report.pdf/my Report.pdf
正确的路径是
/ SD卡/我Report.pdf
请注意,我搜索了计算器,但发现得到的图像或视频,的文件路径的例子有一个如何得到的文件路径的情况下PDF没有例子?
我的code,不是所有的code,但只有PDF部分
公共无效openPDF(视图v)
{
意向意图=新的意图();
//intent.setType("pdf/*);
intent.setType(应用程序/ PDF格式);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(意向,选择全文),SELECT_PDF_DIALOG);
}
公共无效onActivityResult(INT申请code,INT结果code,意向结果)
{
如果(结果code == RESULT_OK)
{
如果(要求code == SELECT_PDF_DIALOG)
{
乌里数据= result.getData();
如果(data.getLastPathSegment()的endsWith(PDF))
{
串pdfPath = data.getPath();
}
其他
{
CommonMethods.ShowMessageBox(CraneTrackActivity.this,无效的文件类型);
}
}
}
}
一些可以请帮我如何从URI得到正确的路径?
myfile文件=新的文件(uri.toString());
myFile.getAbsolutePath()
应该返回û的正确路径
修改
由于@Tron建议工作code是
myfile文件=新的文件(uri.getPath());
myFile.getAbsolutePath()
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?
File myFile = new File(uri.toString());
myFile.getAbsolutePath()
should return u the correct path
EDIT
As @Tron suggested the working code is
File myFile = new File(uri.getPath());
myFile.getAbsolutePath()
这篇关于如何从URL中的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!