How to create a directory in Android?

前端 未结 4 969
礼貌的吻别
礼貌的吻别 2021-01-13 05:16

Everything is in the question. Here is my code :

    private void createDirectory(File currentDirectory) {
  File f = null;
  try {

   f = new File(current         


        
相关标签:
4条回答
  • 2021-01-13 05:19

    You have to add this permission:

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

    By the way, not sure how you are getting the SDcard directory... but it must be this way:

    File sdDir = Environment.getExternalStorageDirectory();
    

    It's important, because some programmers use to think that the SDCard is always /sdcard, but it could change.

    0 讨论(0)
  • 2021-01-13 05:27

    instead of boolean success = f.mkdir(); use

    boolean success = f.mkdirs();
    

    that solved my problem (i used the same reference code as you and had the same issue).

    0 讨论(0)
  • 2021-01-13 05:37

    You're going to need to add

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

    to your manifest file to tell Android to ask the user for your application to be allowed to.

    0 讨论(0)
  • 2021-01-13 05:41

    First, please use the File constructor that takes a File for your directory and a String for your desired subdirectory.

    Beyond that, most likely you are attempting to create a directory where you are not allowed, either because:

    • currentDirectory does not exist, or
    • currentDirectory points to a place on the on-board flash storage where you are not allowed to write, or
    • currentDirectory points to a place on the external storage and you lack the WRITE_EXTERNAL_STORAGE permission
    0 讨论(0)
提交回复
热议问题