问题
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