在Android Studio中简单创建SDCARD下的文件夹

时间:2015-03-12 10:00:43

标签: android android-studio

我已使用该代码在SD卡下创建文件夹。但该文件夹永远不会创建。我已添加了uses-permission             机器人:名称=" android.permission.WRITE_EXTERNAL_STORAGE"表现出来。

 File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Images");
       folder.mkdirs();


       if(folder.exists()){
           Toast.makeText(getApplicationContext(),"exists",Toast.LENGTH_LONG).show();
       }else{

           Toast.makeText(getApplicationContext()," not exists",Toast.LENGTH_LONG).show();
       }

5 个答案:

答案 0 :(得分:0)

你还应该添加:

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

答案 1 :(得分:0)

File directory = new File(pathname);
                if (directory != null && !directory.exists()
                        && !directory.mkdirs()) {
                    try {
                        throw new IOException("Cannot create dir "
                                + directory.getAbsolutePath());
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }

答案 2 :(得分:0)

尝试添加此代码以调试您是否可以实际写入外部存储:

Log.d("MyActivity", "Can write: " + Environment.getExternalStorageDirectory().canWrite());

并验证mkdirs的结果:

boolean result = folder.mkdirs();
Log.d("MyActivity", "mkdirs: " + result);

答案 3 :(得分:0)

试试这个并检查Log的流程 - &gt;

      //First check if the sd card is mounted or not      
         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

                {
                   // sd card mounted
                   Log.d("SDCard", "SDcard is present");
                }  

                File folder = new File(Environment.getExternalStorageDirectory() + File.separator +"Images/");
             if(!folder.exists())

              {
                folder.mkdirs();
                Log.d("SDcard", "Folder created");
              }   
            else
         {
                Log.d("SDCard", "Folder already exists");
         }       
     }

答案 4 :(得分:0)

问题解决了!我在清单的错误部分添加了块。它应该在该部分之前或之后。