How do I check if a directory exist on the sdcard in android?
如何检查sdcard中是否存在一个目录?
6 个解决方案
#1
106
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 here for more methods you might find useful.
在这里查看更多您可能会发现有用的方法。
#2
36
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
// do something here
}
#3
8
The following code also works for java files:
以下代码也适用于java文件:
// Create file upload directory if it doesn't exist
if (!sdcarddir.exists())
sdcarddir.mkdir();
#4
1
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!!'
}
#5
0
I've made my mistake about checking file/ directory. Indeed, you just need to call isFile()
or isDirectory()
. Here is the docs
我在检查文件/目录时犯了一个错误。实际上,您只需要调用isFile()或isDirectory()。这是文档
You don't need to call exists()
if you ever call isFile()
or isDirectory()
.
如果调用isFile()或isDirectory(),则不需要调用exist()。
#6
0
Yup tried a lot, beneath code helps me :)
Yup尝试了很多,下面的代码帮助了我:)
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();
}
#1
106
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 here for more methods you might find useful.
在这里查看更多您可能会发现有用的方法。
#2
36
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
// do something here
}
#3
8
The following code also works for java files:
以下代码也适用于java文件:
// Create file upload directory if it doesn't exist
if (!sdcarddir.exists())
sdcarddir.mkdir();
#4
1
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!!'
}
#5
0
I've made my mistake about checking file/ directory. Indeed, you just need to call isFile()
or isDirectory()
. Here is the docs
我在检查文件/目录时犯了一个错误。实际上,您只需要调用isFile()或isDirectory()。这是文档
You don't need to call exists()
if you ever call isFile()
or isDirectory()
.
如果调用isFile()或isDirectory(),则不需要调用exist()。
#6
0
Yup tried a lot, beneath code helps me :)
Yup尝试了很多,下面的代码帮助了我:)
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();
}