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?

Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
BABU K
  • 917
  • 13
  • 35

1 Answers1

-2

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

Aditya
  • 371
  • 3
  • 8