Unable to create a folder programatically

拜拜、爱过 提交于 2019-11-28 12:23:49

问题


Below is the code that I'm using

String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
File filepathname = new File(root+"/newfolder");
if(filepathname.mkdir())
    Toast.makeText(this,"directory created", Toast.LENGTH_SHORT).show();
else
    Toast.makeText(this,"directory not created", Toast.LENGTH_SHORT).show();

I tried this code in Moto E (running Lollipop) and Nexus 5 (running marshmallow). Folder gets created in Moto but not in Nexus 5.

I have been able to create folder in Nexus 5 before upgrading to Marshmallow.

Is this a known problem in android M? Is there a work around? Or can anyone spot any problem in the code.

Notes:

  1. The above mentioned code is in the onCreate() of my launcher activity,

  2. The below permission is also there in the manifest file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
  3. "newfolder" does not exist already.


回答1:


In my opinion you dont have rights to create folder. If your application target is 23 or above you have to read about new runtime permissions.

Here you can find official documentation about it.



来源:https://stackoverflow.com/questions/33431277/unable-to-create-a-folder-programatically

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