Android mkdirs() sdcard do not work

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 19:34:31

问题


I want to make dir in Sdcard, and i do follow:

  1. I added: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in manifest.
  2. I get root_path by: public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/"; and it returns /storage/emulated/0/Hello_World (getting when debug).

Next, I run this code:

File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
    file.mkdirs();
    Log.e("mkdirs", "" + i++);
}

I also tried both mkdirs() and mkdir() but it's showing endless loop in logcat (Log.e("mkdirs", "" + i++);). Sometimes it work, but sometimes not. Thanks for you helping!
Update: I tried my code for some devices: Nexus4, nexus7, Vega Iron, Genymotion, LG G Pro, then just Vega Iron work as expected. ??!!?!?


回答1:


Try like this it will create a folder in the sd card

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");    
myDir.mkdirs();

If you want to check that file exists or not use this code

File file = new File (myDir, file_name);
if (file.exists ()) 
   // file exist 
else 
   // file not exist  

For reference look at this answer Android saving file to external storage




回答2:


The error is cause by && in while (!file.isDirectory() && !file.mkdirs()) it should be while (!file.isDirectory() || !file.mkdirs()). You should also check if the media is mounted.

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        if (DEBUG) {Log.d(TAG, "createSoundDir: media mounted");} //$NON-NLS-1$
        File externalStorage = Environment.getExternalStorageDirectory();
        if (externalStorage != null)
        {
            String externalStoragePath = externalStorage.getAbsolutePath();
            File soundPathDir = new File(externalStoragePath + File.separator + "Hello_World"); //$NON-NLS-1$

            if (soundPathDir.isDirectory() || soundPathDir.mkdirs())
            {
                String soundPath = soundPathDir.getAbsolutePath() + File.separator;
                if (DEBUG) {Log.d(TAG, "soundPath = " + soundPath);} //$NON-NLS-1$

            }
        }
    }

Cut and paste from one of my project.




回答3:


Thank all you guys, finally i found out the problem. The problem is in the while() loop, I replace by

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && !file.isDirectory()) {
file.mkdirs();
}




回答4:


Use Environment.getExternalStorageDirectory().getAbsolutePath() as below...

public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hello_World/";

And Check that SDCard is mounted before creating directory as below....

File file = new File(Constants.ROOT_PATH);
int i = 0;

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      if(!file.exists()) {
          file.mkdir();
      }
}


来源:https://stackoverflow.com/questions/22495495/android-mkdirs-sdcard-do-not-work

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