【问题标题】:How to search a xml file from android mobile sd card如何从 android 移动 sd 卡中搜索 xml 文件
【发布时间】:2013-01-16 06:54:58
【问题描述】:

我的 SD 卡中有一个 XML 文件。我想搜索整个 sd 卡并找到该文件。 之后我必须解析那个文件。

我在下面做了一个类似的操作。但无法获取文件..

static final String PATH = Environment.getExternalStorageDirectory()
        .getPath() + "/download/";
static final String FILE_NAME = "aura_key.xml";

static File isKeyExist() {
     File file = new File(PATH+FILE_NAME);

     if(file.exists())
     {
         return file;
     }else

    return file;

}

提前致谢。

【问题讨论】:

  • 检查您在日志中的完整路径以确认正确的路径。顺便说一句,您遇到了什么错误?
  • 我得到了 null.. 没有日志猫错误
  • 获取空且没有日志猫错误?哇
  • @Pratik 如果你有这方面的代码,请帮助我

标签: android file xml-parsing sd-card


【解决方案1】:

如果要在整个外部存储中搜索,则需要查看所有目录和子目录等...

这是一个示例解决方案,只需使用您的文件名调用第一个方法(包括您的情况下的文件扩展名:

searchForFileInExternalStorage("aura_key.xml");

这里是方法:

public File searchForFileInExternalStorage(String filename) {
    File storage = Environment.getExternalStorageDirectory();

    return searchForFileInFolder(filename, storage);
}

public File searchForFileInFolder(String filename, File folder) {
    File[] children = folder.listFiles();
    File result;

    for (File child : children) {
        if (child.isDirectory()) {
            result = searchForFileInFolder(filename, child);
            if (result != null) {
                return result;
            }
        } else {
            // replace equals by equalsIgnoreCase if you want to ignore the
            // case of the file name
            if (child.getName().equals(filename)) {
                return child;
            }
        }
    }

    return null;
}

【讨论】:

    【解决方案2】:
    public static boolean checkdirAvail(String yourpath) {
        boolean ret = true;
    
        File file = new File(Environment.getExternalStorageDirectory(), path);
        if (!file.exists()) {
            if (!file.mkdirs()) {
                Log.e("TravellerLog :: ", "Problem creating Image folder");
                ret = false;
            }
        }
        return ret;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      相关资源
      最近更新 更多