检查android SD卡上是否存在目录

发布于 2024-08-28 18:50:41 字数 30 浏览 4 评论 0原文

android 如何检查SD卡上是否存在目录?

How do I check if a directory exist on the sdcard in android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静赏你的温柔 2024-09-04 18:50:41

常规 Java 文件 IO:

File f = new File(Environment.getExternalStorageDirectory() + "/somedir");
if(f.isDirectory()) {
   ....

可能还需要检查 f.exists(),因为如果它存在,并且 isDirectory() 返回 false,则会遇到问题。还有 isReadable()...

检查此处 了解更多您可能会发现有用的方法。

Regular Java file 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()...

Check here for more methods you might find useful.

几度春秋 2024-09-04 18:50:41
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}
疑心病 2024-09-04 18:50:41

以下代码也适用于 java 文件:

// Create file upload directory if it doesn't exist    
if (!sdcarddir.exists())
   sdcarddir.mkdir();

The following code also works for java files:

// Create file upload directory if it doesn't exist    
if (!sdcarddir.exists())
   sdcarddir.mkdir();
噩梦成真你也成魔 2024-09-04 18:50:41

一般使用此函数来检查目录是否存在:

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;
  }

使用如下函数:

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!!'
}

General use this function for checking is a Dir exists:

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!!'
}
雨后咖啡店 2024-09-04 18:50:41

我在检查文件/目录时犯了错误。事实上,您只需要调用 isFile()isDirectory() 即可。这是文档

如果您曾调用 isFile()isDirectory(),则无需调用 exists()

I've made my mistake about checking file/ directory. Indeed, you just need to call isFile() or isDirectory(). Here is the docs

You don't need to call exists() if you ever call isFile() or isDirectory().

桃扇骨 2024-09-04 18:50:41

是的,尝试了很多,下面的代码对我有帮助:)

 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();
                }

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();
                }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文