2012-01-16 247 views
34

请在下面找到我的代码。我需要获得用户从SDcard中选择的pdf文档的文件路径。问题是,URI.getPath()返回:如何从URI获取文件路径?

/file:///mnt/sdcard/my%20Report.pdf/my Report.pdf 

正确的路径是

/sdcard/my Report.pdf 

请注意,我搜索了计算器,但发现得到的文件路径的例子图像或视频的,没有如何得到的文件路径在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的正确路径?

+0

这里的可共享的ContentProvider得到它是正确答案的问题 [看看] [1] [1]:HTTP:/ /stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore – sheetal 2013-02-20 07:59:16

回答

21
File myFile = new File(uri.toString()); 
myFile.getAbsolutePath() 

应该返回u对应的正确路径

编辑

由于@Tron建议的工作代码为

File myFile = new File(uri.getPath()); 
myFile.getAbsolutePath() 
+3

我没有FILE这里,我需要从乌里得到它。 – 2012-01-17 09:48:43

+0

看到编辑答案 – 2012-01-17 09:50:33

+0

它的返回: /content:/com.quickoffice.mx.samsung/file%3A%2F%2F%2Fmnt%2Fsdcard%2FMy%2520Report.pdf/My%20Report.pdf?MIME-TYPE=申请%2Fpdf – 2012-01-17 10:31:10

4

这里是问题的答案 here

实际上,我们必须从摄像机应用