'Get filePath for Recents,Downloads,Images from Uri of ACTION_GET_CONTENT Intent android

I am invoking image picker in my android app using intent ACTION_GET_CONTENT. In onActivityResult() I get Uri using data.getData().

Now I am using an already existing code to compress this image but that requires filePath to the image.

I am getting null while parsing path from Uri for Recents, Downloads, Images.

I use projection MediaStore.Images.Media.DATA on contentResolver.query() with Uri. It fetched correct filePath for all other options except Recents, Downloads, Images.

How can I make these work ? The solution should work on latest Android version.

Uris not working:

Downloads- content://com.android.providers.downloads.documents/document/msf%3A2375

Recents - content://com.android.providers.media.documents/document/msf%3A2375

Images-

content://com.android.providers.media.documents/document/msf%3A2375

External Storage -

content://com.android.externalstorage.documents/document/primary%3ADownload%2Fimages%20.jpeg

Uri working:

content://media/external/images/media/2165



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source