Create file failed from mkdir()

ぃ、小莉子 提交于 2020-01-16 09:18:07

问题


I try to create file in my android emulator, but when i finish my code. I can't find the file i create from android device monitor.

Here is my code:

try {
    if (Environment.getExternalStorageState()
            .equals(Environment.MEDIA_MOUNTED)) {
        System.out.println("can be read and write");
        File sdFile = android.os.Environment.getExternalStorageDirectory();

        //String path = sdFile.getPath() + File.separator + "DestPdf";
        String path = sdFile.getPath() + "/demos/file/tmp/test";
        File dirFile = new File(path);

        if (!dirFile.exists()) { // if file doesn't exist
            System.out.println("create file");
            dirFile.mkdir(); // create file
            System.out.println(dirFile.toString());
        }
    }
} catch (Exception ex) {
    ex.toString();
}

I also add some permissions:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

Here is my print out, i can see the root /storage/sdcard/demos/file/tmp/test

11-17 08:38:10.263 15501-15501/? I/System.out: can be read and write
11-17 08:38:10.263 15501-15501/? I/System.out: create file
11-17 08:38:10.264 15501-15501/? I/System.out: /storage/sdcard/demos/file/tmp/test

But i can't find the file from android device monitor

What step i miss it ? Any help would be appreciated . Thanks in advance.


回答1:


you should replace

dirFile.mkdir();

to

dirFile.mkdirs();

Example:

if (!dirFile.exists()) { // if file doesn't exist
        System.out.println(dirFile.mkdir());
        System.out.println(dirFile.mkdirs());

    }

will yield false for the first [and no dir will be created], and true for the second, and you will have created /demos/file/tmp/test

mkdirs() also creates parent directories in the path this File represents.

javadocs for mkdirs():

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

javadocs for mkdir():

Creates the directory named by this abstract pathname.

hope help you!




回答2:


For Android 6+ you need to add code to ask the user to confirm the permissions you asked in manifest.

Google for runtime permissions.




回答3:


I had the same problem a few months ago.

In my case the file was there, but windows couldn't see it (or the integration between adb and windows explorer).

Resetting the MOBILE was the only solution I found... After the reset the file was there in Windows Explorer.

I recommend you to try to find your files using the cli command:

adb shell ls sdcard/"your_path"                                                                                           

Also, try to create it using:

.getAbsolutePath()

Like this:

File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "YOUR_FOLDER_NAME");

if(!path.exists()) 
{
    path.mkdirs();
} 
else 
{ 
    Log.d ("TAG","Path already exists");
}

Hope it helps



来源:https://stackoverflow.com/questions/47346490/create-file-failed-from-mkdir

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