Home > Java > javaTutorial > body text

How to Resolve Directory Creation Issues in Android Marshmallow and Beyond

Linda Hamilton
Release: 2024-10-24 03:25:31
Original
397 people have browsed it

How to Resolve Directory Creation Issues in Android Marshmallow and Beyond

Directory Creation Problems in Android Marshmallow and Beyond

Android Marshmallow introduced substantial changes to the way applications interact with the file system, causing issues with directory creation for some native applications. When attempting to create a directory in Marshmallow, some users experience inconsistent behavior, with the operation working sometimes and failing others.

Understanding the Issue

The root cause of this problem lies in Android's new Storage Access Framework (SAF). Prior to Marshmallow, applications had unrestricted access to the internal and external storage, allowing them to create directories freely.

However, SAF introduces permissions that restrict write access to the external storage by default. As a result, applications must explicitly request write permissions before attempting to create directories on the external storage.

Solution: Request Write Permission

To resolve the directory creation issue, applications must request the WRITE_EXTERNAL_STORAGE permission from the user before attempting to create directories. This permission ensures that the application has the necessary authorization to modify the external storage.

Here's an example code that shows how to request and check for the storage write permission:

<code class="java">import android.Manifest;
import android.content.pm.PackageManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


public class MyDevIDS extends AppCompatActivity {

        private static final int REQUEST_RUNTIME_PERMISSION = 123;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (CheckPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // you have permission go ahead
                createApplicationFolder();
            } else {
                // you do not have permission go request runtime permissions
                RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
            }
        }

        @Override
        public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

            switch (permsRequestCode) {

                case REQUEST_RUNTIME_PERMISSION: {
                    if (grantResults.length > 0
                            &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        // you have permission go ahead
                        createApplicationFolder();
                    } else {
                        // you do not have permission show toast.
                    }
                    return;
                }
            }
        }

        public void RequestPermission(Activity thisActivity, String Permission, int Code) {
            if (ContextCompat.checkSelfPermission(thisActivity,
                    Permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        Permission)) {
                } else {
                    ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Permission},
                            Code);
                }
            }
        }

        public boolean CheckPermission(Context context, String Permission) {
            if (ContextCompat.checkSelfPermission(context,
                    Permission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }
    }</code>
Copy after login

Once the permission is granted, applications can proceed to create directories using the mkdirs() method without encountering issues. By implementing this solution, developers can ensure that their applications can reliably create directories in Android Marshmallow and later versions.

The above is the detailed content of How to Resolve Directory Creation Issues in Android Marshmallow and Beyond. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!