1

I am fetching a document Uri from the server which points out to where the document (pdf or word) was initially stored in the phone storage at the time of upload. In my app, I would want to check if the document still exists in the user's phone storage (by checking if this uri is still valid) before downloading the document. How can I do this?I've tried multiple ways to no success. Please help. This is what I've already tried.

 Uri uri = Uri.parse("content://com.android.providers.downloads.documments/document/748");
            final File f = new File(uri.getPath());
           if (f.exists()){
                Log.d("File existence", "Yes, file exists");
            }else {
                Log.d("File does not exist", "so sorry");
            }

I also tried implementing this using ContentResolver as pointed out in this similar question but the method threw a security exception: querying the cursor requires manage documents permission which when added to the manifest also brought an error, that the permission is only used by system apps. Please help. I'm running the app in android 6.0.1 Thanks in advance

Ezra Raleigh
  • 61
  • 12

1 Answers1

0

Try this out :

getContext().getContentResolver().getPersistedUriPermissions().forEach( {element -> element.uri == yourUri});
haresh
  • 1,424
  • 2
  • 12
  • 18