Android mkdirs() returns false
I am having an Android mkdirs() return false. I read another response you had to a similar question. You mentioned something about Environment.getExternal ... anyways I am running Android 2.2 on Droid X. The sd card is mounted and both return values for isreadable and iswritable return true. here is my code ...
public void writeToExternalStoragePublic(String filename, byte[] content) {
// API Level 7 or lower, use getExternalStorageDirectory()
// to open a File that represents the root of the external
// storage, but writing to root is not recommended, and instead
// application should write to application-specific directory, as shown below.
String packageName = this.getPackageName();
Toast.makeText(this, packageName, 1000).show();
String path 开发者_StackOverflow社区= "/Android/data/";// + packageName + "/files/";
if (isExternalStorageAvailable() &&
!isExternalStorageReadOnly()) {
try {
File file = new File(path, filename);
file.mkdirs();
file.mkd
Toast.makeText(this, "Made Dirs = " + file.mkdirs(), 1000).show();
Toast.makeText(this, "Made Dir = " + file.mkdir(), 1000).show();
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
Toast.makeText(this, "success", 1000).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "fnf", 1000).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "io", 1000).show();
} catch (SecurityException e) {
Toast.makeText(this, "security", 1000).show();
}
}
}
Thank You. If you know any way I could get it to mkdirs I would appreciate it. Thank You.
This code was actually an example from someone else and I am trying to make it work.
http://www.ibm.com/developerworks/xml/library/x-androidstorage/index.html
D-RAN
File.mkdirs()
will only return true once: when the directories actually had to be created. From the documentation:
returns true if the necessary directories have been created, false if the target directory already exists or one of the directories can not be created.
Therefore unless you delete the directory, it would return false() every other time. If you want to know whether the directory exists, use File.isDirectory()
FWIW, I don't have a DroidX so I can't speak to the path you're using, but that path wouldn't work on the devices I have. I'd also recommend checking the path (using ADB shell) to make sure the path you're trying to create is a valid one.
精彩评论