在android sdcard中创建一个目录

时间:2013-03-12 05:01:43

标签: android file-io sd-card android-sdcard

我试图通过这张卡在sd卡中创建目录。但是它没有工作。虽然它没有工作,但是在清单中写了外部存储权限。需要紧急帮助。我把这个代码放在try catch中。但它没有进入catch block.here是我的代码..

try
        {
          File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
           if(!songDirectory.exists())
          {
              songDirectory.mkdirs();
              Toast.makeText(getApplicationContext(),"Directory created", Toast.LENGTH_LONG).show();
                // ShowlistView();
          }
         else
         {
               //ShowlistView();
               Toast.makeText(getApplicationContext(),"Directory AlreadyExists", Toast.LENGTH_LONG).show();
         }
        }
        catch(Exception e)
        {
            Log.e("","Error While creating file is:::::"+e+"");
        }

3 个答案:

答案 0 :(得分:0)

试用以下代码:

File sdDir = Environment.getExternalStorageDirectory();
            File wwwjdicDir = new File(sdDir.getAbsolutePath() + "/your_folder_name");
            if (!wwwjdicDir.exists()) {
                wwwjdicDir.mkdir();
            }

            if (!wwwjdicDir.canWrite()) {
                return;
            }

答案 1 :(得分:0)

尝试

       File dir = new File(DEFAULT_STORAGE_LOCATION);
        // test dir for existence and writeability
        if (!dir.exists()) {
            try {
                dir.mkdirs();
            } catch (Exception e) {   
                return null;
            }
        } else {
            if (!dir.canWrite()) {
                   return null;
                 // do your work here
            }
        }

答案 2 :(得分:-1)

如果这是您的代码

File songDirectory = new `File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");

您需要将其更改为此

File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");