3

I want to get the real path of PDF from URI of the downloaded pdf from the download folder.

I am already following this link. But in this, I do not get the real path and getting exception. like below:

URI

content://com.android.providers.downloads.documents/document/msf%3A26

Lunch Intent:

 Intent intent = new Intent();
 intent.setType("application/pdf");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select PDF"), PICK_PDF_REQUEST);

OnActivityResult:

if (requestCode == PICK_PDF_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                String path = null;
                try {
                    path = getPDFPath(uri);
                    LogUtils.logE(TAG, "Document :  " + path);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
          }
 }


     private String getPDFPath(Uri uri) {
            // DocumentProvider
            if (DocumentsContract.isDocumentUri(context, uri)) {
                // ExternalStorageProvider
                if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
                    String documentId = DocumentsContract.getDocumentId(uri);
                    String[] split = documentId.split(":");
                    String type = split[0];
                    return Environment.getExternalStorageDirectory().toString() + "/" + split[1];

                } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                    String decodedURI = Uri.decode(uri.toString());

                    if (decodedURI.contains("raw:")) {
                        return decodedURI.substring(decodedURI.indexOf("raw:") + 4);
                    }

                    String id = DocumentsContract.getDocumentId(Uri.parse(decodedURI));

                    Uri contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id));

                    return getDataColumn(contentUri, null, null);
                }
            }
            return null;
        }

        public String getDataColumn(Uri uri, String selection,
                                    String[] selectionArgs) {

            Cursor cursor = null;
            final String column = "_data";
            final String[] projection = {
                    column
            };

            try {
                cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                        null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int index = cursor.getColumnIndexOrThrow(column);
                    return cursor.getString(index);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
            return null;
        }

Exception

 2020-05-04 14:47:09.747 17908-17908/ W/System.err: java.lang.NumberFormatException: For input string: "msf:26"
2020-05-04 14:47:09.748 17908-17908/W/System.err:     at java.lang.Long.parseLong(Long.java:594)
 2020-05-04 14:47:09.748 17908-17908/ W/System.err:     at java.lang.Long.valueOf(Long.java:808)

I want Real path of PDF so I can use it for further use.

Thanks in Advance!!!

Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34

1 Answers1

2

No need to get a 'real' path.

There is no need for it.

Just use the uri as is.

We all do like this these days.

Join us.

blackapps
  • 8,011
  • 2
  • 11
  • 25