Home  >  Article  >  Java  >  What is the issue with creating a directory in Android Marshmallow?

What is the issue with creating a directory in Android Marshmallow?

DDD
DDDOriginal
2024-10-24 03:22:46464browse

What is the issue with creating a directory in Android Marshmallow?

Issue creating directory in Android Marshmallow

In Android 5.0, creating directory using native app works fine. But in Android 6.0 (Marshmallow), sometimes it works and sometimes it doesn't. Below is the code on how to create a new directory. Directory is used to save video and image files.

<code class="java">public static void createApplicationFolder() {
    File f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME);
    f.mkdirs();
    f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_COMPRESSED_VIDEOS_DIR);
    f.mkdirs();
    f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_TEMP_DIR);
    f.mkdirs();

}</code>

Solution:

In order to solve this problem, you need to add this permission in the manifest file:

<code class="xml"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></code>

Also need to Runtime permissions are requested when the application is first run. Here is the modified code:

<code class="java">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)) {
            // 具有权限,继续执行
            createApplicationFolder();
        } else {
            // 没有权限,请求运行时权限
            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
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 具有权限,继续执行
                    createApplicationFolder();
                } else {
                    // 没有权限,显示吐司
                }
                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>

After adding this code, permission to create a directory will always be requested the first time the application is run.

The above is the detailed content of What is the issue with creating a directory in Android Marshmallow?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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