Check write permissions on external storage in an Android application

The usual way to do this is to check if the manifestresolution is using WRITE_EXTERNAL_STORAGE:

String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);

My question is using the method canWrite(). The docs say:

public boolean canWrite ()
 Added to API Level 1
Indicates the current context is allowed to write to this file.

Thus, we can use this method to check the permission as follows:

Environment.getExternalStorageDirectory().canWrite();

This method seems simpler than the first, and also does not need to be used Context. We can also use it for verification read permission. But is this a good way to test them? Also is this a good way to check if the manifest uses read / write permissions?

+4

Source: https://habr.com/ru/post/1588912/


All Articles