检查android的sdcard上是否存在目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2625419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if directory exist on android's sdcard
提问by Alxandr
How do I check if a directory exist on the sdcard in android?
如何检查android中sdcard上是否存在目录?
回答by synic
Regular Java file IO:
常规Java文件IO:
File f = new File(Environment.getExternalStorageDirectory() + "/somedir");
if(f.isDirectory()) {
....
Might also want to check f.exists()
, because if it exists, and isDirectory()
returns false, you'll have a problem. There's also isReadable()
...
可能还想检查f.exists()
,因为如果它存在,并isDirectory()
返回 false,你就会遇到问题。还有isReadable()
...
Check herefor more methods you might find useful.
请在这里为您可能会发现更多有用的方法。
回答by Mark B
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
// do something here
}
回答by Artail3
The following code also works for java files:
以下代码也适用于 java 文件:
// Create file upload directory if it doesn't exist
if (!sdcarddir.exists())
sdcarddir.mkdir();
回答by Ingo
General use this function for checking is a Dir exists:
一般使用这个函数来检查一个 Dir 是否存在:
public boolean dir_exists(String dir_path)
{
boolean ret = false;
File dir = new File(dir_path);
if(dir.exists() && dir.isDirectory())
ret = true;
return ret;
}
Use the Function like:
使用函数,如:
String dir_path = Environment.getExternalStorageDirectory() + "//mydirectory//";
if (!dir_exists(dir_path)){
File directory = new File(dir_path);
directory.mkdirs();
}
if (dir_exists(dir_path)){
// 'Dir exists'
}else{
// Display Errormessage 'Dir could not creat!!'
}
回答by Ingo
回答by Agilanbu
Yup tried a lot, beneath code helps me :)
是的,尝试了很多,下面的代码对我有帮助:)
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "ur directory name");
if (!folder.exists()) {
Log.e("Not Found Dir", "Not Found Dir ");
} else {
Log.e("Found Dir", "Found Dir " );
Toast.makeText(getApplicationContext(),"Directory is already exist" ,Toast.LENGTH_SHORT).show();
}