I follow this guide to ask for permissions for Android 6.0, https://developer.android.com/preview/features/runtime-permissions.html#support-lib
However, I have to destroy my application and re-launch to actually have the WRITE_EXTERNAL_STORAGE
permission, otherwise it keeps failing for file creation.
Am I missing something?
if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions((MainActivity)mContext,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
Intent i = new Intent(v.getContext(), ServiceDownload.class);
v.getContext().startService(i);
}
File is created in the service and this is emulator not a real device.
Android provides a utility method, shouldShowRequestPermissionRationale() , that returns true if the user has previously denied the request, and returns false if a user has denied a permission and selected the Don't ask again option in the permission request dialog, or if a device policy prohibits the permission.
You need to declare the permissions in the manifest file first before you request for them programmatically. refer this for more information. declaring the permission in the manifest file is static type declaration by which your os know what kind of permission your app might require.
checkSelfPermission(activity, Manifest. permission. X) checks if any permission is already granted, if you check out other answers they do the same, and rest of the code asks for the permissions not granted.
It is a bug as someone already mentioned, but I found in this question that you can add this line:
android.os.Process.killProcess(android.os.Process.myPid());
after your permission has been granted. This is not a real fix, just helps avoid crashing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With