So after debugging, I found a way to get the path of the picked folder.
First, I build uri using the document tree
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
DocumentsContract.getTreeDocumentId(uri));
and performed a query on it
cursor = context.getContentResolver().query(contentUri, null, null, null, null);
and get the data in document_id
column
String string = cursor.getString(cursor.getColumnIndex("document_id"));
it will be something like XXX-XXX:DCIM/ffmpeg
if it's in the sd card, or primary:DCIM/ffmpeg
if it's in the internal storage.
In the end, I was able to get the real path by using this:
String[] strings = string.split(":");
String newPath = "/storage/" +
(strings[0].equals("primary")?"emulated/0/"+strings[1]:
string.replaceAll(":", "/"));
This will split the data in the column into two part (I'm interested in the first string only to know if it's in sd card or not). If it's in the sd card, it will add a prefix /storage/
and replace all colons with a forward slashes, if it's in the primary storage, it will add a prefix /storage/emulated/0/
and replace the colons with forward slashes.
I have tested it with many devices (even the emulator) and it worked fine.