getExternalFilesDir permissions

纵然是瞬间 提交于 2020-07-08 06:15:25

问题


My application is live on Play Store, I noticed that I was not asking permissions when calling:

File savedImagesDirectory = getBaseContext.getExternalFilesDir("SavedImages");
if(savedImagesDirectory != null) {
    if (!savedImagesDirectory.exists()) {
        if (savedImagesDirectory.mkdir()) ; //directory is created;
    }
}

Strange thing is, I'm not getting any crashes when calling this without runtime permissions. I tested on my device running Nougat and the files are created and I experienced no crashes.

I only have permissions in my Manifest, but I don't ask runtime permissions.

I've seen A LOT of answers on SO that say that runtime permissions should be asked when creating a folder/file in external file directory.


My question:

Should I ask runtime permissions when calling the above? If yes, why am I not experiencing any crashes?

It would also be helpful if someone can provide a link where I can read up about this.


回答1:


Although it returns path to external storage, you don't need permission to access directory returned by getExternalFilesDir().

It is same as getFilesDir() but it is public to other apps as well.

Also note that if you write anything in that directory and clear app data or uninstall the app, everything will be deleted.

For more information, read the documentation at https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)




回答2:


String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, WRITE_REQUEST_CODE);

use this code

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] 
grantResults) {
switch (requestCode) {
   case WRITE_REQUEST_CODE:
     if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
       //Granted.


     }
   else{
       //Denied.
     }
    break;
}
}


来源:https://stackoverflow.com/questions/52437112/getexternalfilesdir-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!