1. Introduction
In Android development, storage permissions are a common and important permission. Different versions of Android systems have different ways of managing and handling storage permissions, which leads to various compatibility problems when dealing with storage permissions. This article will introduce in detail the changes in storage permissions of each version of Android, as well as how to handle it compatiblely, and give specific code examples.
2. Changes in storage permissions for each version of Android
2.1 Before Android 6.0 (API Level 23)
Prior to Android 6.0, the permissions of the app were granted at one time at installation. As long as the user installs the application, the application has itAll permissions declared in do not need to be requested again at runtime. For example, to use storage permissions, just
Add the following code to:
<uses-permission android:name=".READ_EXTERNAL_STORAGE" /> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
2.2 Android 10 (API level 29) and later
Android 10 introduced a Scoped Storage mechanism, which made significant changes to the way applications access external storage. By default, the application can only access its own exclusive directory (such as()
Returned directory) and media files, accessing files of other applications requires special processing. at the same time,WRITE_EXTERNAL_STORAGE
Permissions are no longer granted to ordinary applications unless the application is inStatement in the
android:requestLegacyExternalStorage="true"
To use the old storage mode.
2.3 Android 11 (API level 30) and later
Android 11 further strengthens the limitations of partition storage.android:requestLegacyExternalStorage
The properties no longer work, the application must adapt to partition storage. However, Android 11 provides some new APIs to handle file access, such asMediaStore
API is used to access media files.
3. Compatibility processing solution and code examples
3.1 Permission Checking and Request
The following is a code description for requesting storage permissions in Android 6.0 and above
import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST_CODE = 1; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Check whether storage permissions have been granted if ((this, .READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // If permission is not granted, request permission (this, new String[]{.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } else { // Permissions have been granted to perform related operations performStorageOperation(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { (requestCode, permissions, grantResults); if (requestCode == PERMISSION_REQUEST_CODE) { if ( > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The user has granted permissions to perform related operations performStorageOperation(); } else { // The user rejected permission and gave a prompt (this, "Storage permission denied, operation cannot be performed", Toast.LENGTH_SHORT).show(); } } } private void performStorageOperation() { // Perform storage-related operations (this, "Start the storage operation", Toast.LENGTH_SHORT).show(); } }
3.2 Partition storage processing for Android 10 and above
In Android 10 and above, using partition storage can be done through()
Access the application's exclusive directory. Here is a simple file reading and writing example:
import ; import ; import ; import ; import ; import ; import ; public class StorageActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_storage); // Get the exclusive directory of the external storage of the application File externalFilesDir = getExternalFilesDir(null); if (externalFilesDir != null) { // Create a file File file = new File(externalFilesDir, ""); try { // Write to the file FileOutputStream fos = new FileOutputStream(file); String content = "Hello, World!"; (()); (); (this, "File writing successfully", Toast.LENGTH_SHORT).show(); } catch (IOException e) { (); (this, "File writing failed", Toast.LENGTH_SHORT).show(); } } } }
3.3 Media file access for Android 11 and above
import ; import ; import ; import ; import ; import ; import ; public class MediaAccessActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_media_access); // Get ContentResolver ContentResolver contentResolver = getContentResolver(); // Define the URI of the query Uri uri = .EXTERNAL_CONTENT_URI; // Define the column of the query String[] projection = {._ID, .DISPLAY_NAME}; // Execute query Cursor cursor = (uri, projection, null, null, null); if (cursor != null) { while (()) { // Get the image ID and file name long id = ((._ID)); String displayName = ((.DISPLAY_NAME)); (this, "Picture ID: " + id + ", file name: " + displayName, Toast.LENGTH_SHORT).show(); } (); } } }
4. Summary
In Android development, handling storage permission compatibility issues requires corresponding handling according to the characteristics of different versions of the system. In Android 6.0 and above, the runtime permission mechanism should be used; in Android 10 and above, it should be adapted to partition storage; in Android 11 and above, the file access should be used using the new API. Through reasonable permission management and file operations, we can ensure that the application can run normally on different versions of Android systems.
The above is the detailed analysis of the Android storage permission compatibility issue. For more information about Android storage permission compatibility, please follow my other related articles!