Search code examples
androidandroid-permissionsandroid-fileandroid-storage

Why does Android allow reading files without permission?


In my app, I wrote the following code to access files:

public void performFileSearch() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);

    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}

In onActivityResult I am receiving Uri of the selected image.

Why am I able to access the Uri without having READ_EXTERNAL_STORAGE permission?

minSdk = 19

compileSdk = 27

targetSdk = 27

Test Android Versions:

Android 4.4.2 API 19

Android 6.0.1 API 23


Solution

  • Why am I able to access the Uri without having READ_EXTERNAL_STORAGE permission?

    Because the user was involved in choosing it. You are granted temporary access to that document (and, if you use takePersistableUriPermissions()), you can have durable access to that document.