4

I need to get file's path to convert it into input stream for uploading purose. So I choose pdf document using file chooser.I get Uri in onActivityResult().Using uri , I am getting file's path.But it does not return correct path. The absolute path:

when choosing file from internal storage /document/primary:pdf/HA License Plate.pdf when choosing file from external storage /document/9016-4EF8:certificates/img002.pdf 

So I could not get correct file path , to convert into inputstream. So please suggest me to get correct file path in my android application? The code I am using is as follows.

Code Is:-

public void ChoosePDF(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"), REQUEST_CODE); } public void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) { if (requestCode == REQUEST_CODE) { Uri data = result.getData(); if (data.getLastPathSegment().endsWith("pdf")) { String pdfPath = data.getPath(); File myFile = new File(data.getPath()); Log.i("FirstActivity", "pdfPath is " + pdfPath); Log.i("FirstActivity", "uri abs path is " + myFile.getAbsolutePath()); } else { Toast.makeText(SplashScreenActivity.this, "Invalid file type.Please choose valid file!", Toast.LENGTH_LONG).show(); } } } } 

Is there any android version specific problem while getting file's path?I am using android lollipop device for testing?

4
  • How did you solve this issue(/document/9016-4EF8:certificates/img002.pdf)? Please share me your answer. Commented Jan 8, 2016 at 14:14
  • @BABUK i am facing the same issue, did u find any solution? Commented Mar 7, 2016 at 10:43
  • Had anybody solved above issue? Please share it will help me too. Commented Mar 22, 2016 at 11:49
  • Its so sad to see that nobody has came up with this answer.... I mean I've seen apps built with this ability are programmers themselves just 'data hiding'? Commented Apr 6, 2016 at 2:56

1 Answer 1

-2

Use File file=new File(new URI(data.toString()));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.