Home >Java >javaTutorial >How to Fix Folder Creation Issues on Android Marshmallow (6.0)?

How to Fix Folder Creation Issues on Android Marshmallow (6.0)?

DDD
DDDOriginal
2024-10-24 03:48:02596browse

How to Fix Folder Creation Issues on Android Marshmallow (6.0)?

Marshmallow Android Folder Creation Issues

Having trouble creating folders on Android 6.0 (Marshmallow)? You've come to the right place!

Some Marshmallow devices exhibit instability in creating new folders. While everything works fine on Android 5.0, on Marshmallow sometimes the creation succeeds and sometimes not. why is that?

Permission issues

Marshmallow introduces tighter control over external storage permissions. For Android M and higher, explicit user permission is required before using external storage. If you do not request this permission, your application will not be able to create the directory.

Solution

In order to successfully create a folder on Marshmallow, you need to request WRITE_EXTERNAL_STORAGE permission from the user. You can achieve it using the following code:

<code class="java">@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    if (CheckPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // 拥有写入权限
        createApplicationFolder();
    } else {
        // 请求运行时权限
        RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
    }
}</code>

CheckPermission and RequestPermission are helper methods that check if the application has the WRITE_EXTERNAL_STORAGE permission and request it if it has not been granted yet.

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

With these changes, your application will be able to reliably create folders on Marshmallow and above.

The above is the detailed content of How to Fix Folder Creation Issues on Android Marshmallow (6.0)?. 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