0

In my application, I let users choose PDF files. My solution worked before but now it doesn't work and I can't find out what causes the problem. The Intent data of the method onActivityResult is now returning a different URI path, which ends with different numbers at the end of the URI when picking any PDF file. My code:

 public void addPDF(View v){

    if (Build.VERSION.SDK_INT <19){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("application/pdf");
        try {
            startActivityForResult(Intent.createChooser(intent, "Choose a pdf file"), 10);
        } catch (android.content.ActivityNotFoundException ex) {
            //show error
        }
    }else{
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("application/pdf");
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(Intent.createChooser(intent, "Choose a pdf file"), 11);
        } catch (android.content.ActivityNotFoundException ex) {
            //show error
        }

    }

}


@Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if ((requestCode == 10 || requestCode == 11) && resultCode == RESULT_OK) {

            Uri pdf_uri = data.getData();

            //WILL RETURN SOMTHING LIKE content://com.android.providers.downloads.documents/document/15
            //or content://com.android.providers.downloads.documents/document/527 depending on different PDF files I pick
            pdf_path = pdf_uri.getPath();
        }
    }

I wonder why the path is received as different numbers (in the end of the path), what are these numbers indicading and how do I solve this issue to get the right path of each PDF file?

AndroidDev
  • 123
  • 9

0 Answers0