How to get the file path from a URI that points to a PDF document?

Add this snippet below in your getPDFPath method:

public String getPDFPath(Uri uri){

     final String id = DocumentsContract.getDocumentId(uri);
     final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

     String[] projection = { MediaStore.Images.Media.DATA };
     Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
     cursor.moveToFirst();
     return cursor.getString(column_index);
}

In your case, this code is specifically for documents from DownloadProvider, for further implementation check Paul Burke’s answer. I personally use his aFileChooser library to avoid this kind of problems.

Leave a Comment