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?
Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().
String path = uri. getPath(); File dir = new File(sdcard. getAbsolutePath()+"/"+path1);
From what I know you can find the path of a file in the internal storage with java simply by using: String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With