summary
I encountered a problem during development today. The project was unable to create a directory on the SD card of Android 6.0 device. I tested 5.0 and 4.3 devices on mobile devices below Android 6.0, which were both normal, but it was not normal in Android 6.0.
After troubleshooting, the following code cannot be executed to create a directory on a device with 6.0.
File dir = new File(DbConfig.BASE_PATH); if (!()) { (); }
Derivative knowledge
At the Google I/O Developer Conference on May 29, 2015, Google released Android M and named it "Marshmallow". For developers, Android 6.0 (API 23) has brought some changes to developers.
Permission management is the biggest change in Android M
Change description:
Permission management is more refined, and has been changed from static authorization at the previous installation time to dynamic authorization at the current runtime.
Everyone has been complaining about Android's permissions for a long time, and Android should be able to greatly improve this problem.
The main changes are:
In the system settings, you can control the permissions of the APP separately and group them according to the content.
Ordinary permissions are still authorized during installation, other permissions are authorized during runtime, and the purpose of using this permission is to be parsed.
For developers, you need to be careful about permission-related issues. When using a certain function, you need to always determine whether you have any permissions to change and request user authorization in a suitable way.
Now describe the solution to the processing
1. Authorization to initiate the storage space of the read and write device
(,new String[]{ .WRITE_EXTERNAL_STORAGE},1);
2 Write permission request to return the function
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { boolean writeAccepted = false; switch (requestCode) { case 1: writeAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; break; } }
3 Execute the directory creation code
if (writeAccepted) { String state = (); if (Environment.MEDIA_MOUNTED.equals(state)) { File dir = new File(DbConfig.BASE_PATH); if (!()) { (); } } }
The above method of Android 6.0 cannot create a directory on SD card is all the content I share with you. I hope you can give you a reference and I hope you can support me more.