如何从URI获取文件路径? [重复]

时间:2022-07-02 19:35:50

This question already has an answer here:

这个问题在这里已有答案:

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:

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

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

The correct path is:

正确的道路是:

/sdcard/my Report.pdf

Please note that i searched on * 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?

请注意我在*上搜索但找到了获取图像或视频的filePath的示例,在PDF的情况下没有如何获取文件路径的示例?

My code , NOT all the code but only the pdf part:

我的代码,不是所有的代码,只有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");   
             }               
          }
      }
 }

Can some please help me how to get the correct path from URI?

有人可以帮助我如何从URI获取正确的路径?

2 个解决方案

#1


21  

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

should return u the correct path

应该返回正确的路径

EDIT

编辑

As @Tron suggested the working code is

正如@Tron建议的那样,工作代码是

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

#2


4  

Here is the answer to the question here

这是这里问题的答案

Actually we have to get it from the sharable ContentProvider of Camera Application.

实际上我们必须从Camera Application的可共享ContentProvider中获取它。

EDIT . Copying answer that worked for me

编辑。复制对我有用的答案

private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;

}

}

#1


21  

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

should return u the correct path

应该返回正确的路径

EDIT

编辑

As @Tron suggested the working code is

正如@Tron建议的那样,工作代码是

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

#2


4  

Here is the answer to the question here

这是这里问题的答案

Actually we have to get it from the sharable ContentProvider of Camera Application.

实际上我们必须从Camera Application的可共享ContentProvider中获取它。

EDIT . Copying answer that worked for me

编辑。复制对我有用的答案

private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;

}

}