android mkdirs not working

心不动则不痛 提交于 2019-11-28 23:22:34

For those not as experienced like me. I fought this issue, lost hair for some time. I am targeting api 21 (for compatibility sake) and it worked on lollipop but on marshmallow it would not create the directory. I did have the "uses" permission in the manifest but it still would not work. Apparently in Marshmallow when you install with Android studio it never asks you if you should give it permission it just quietly fails, like you denied it. You must go into Settings, apps, select your application and flip the permission switch on.

IDIOT ME! i have used the Manifest Permission but when installed the app on phone i didnt grant permission for storage!... i understand a negative on this question... but i hope if someone else face the same..check your phone permission. sorry all for inconvenience.

if you are testing on android M, you should probably check Settings > App > Permission to see if permission to access storage is granted. This saved me.

Did you put

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

in your AndroidManifest? If you are using android M you must request user permission to write on sd, look here an example

you have created directory, not file. Create new file with following code

File file = new File(dir.getAbsolutePath() + "/Pic.jpg");
file.createNewFile()

Try this. Provide runtime permission for marshmallow it is perfectly work in my Application code :

 private String getFilename(String strFileName) {
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File fileBase = new File(filepath, "Test");
        if (!fileBase.exists()) {
            fileBase.mkdirs();
        }
        return (file.getAbsolutePath() + "/" + strFileName + file_exts[currentFormat]);
    }

new File(getFilename(edt.getText().toString().trim()))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!