【问题标题】:Android: Get files from specific folder into a string arrayAndroid:将特定文件夹中的文件获取到字符串数组中
【发布时间】:2017-08-18 19:42:14
【问题描述】:

我知道这个问题已经回答了好几次,但似乎没有任何答案有帮助。我尝试过使用多种方法,最近是这样的:

Getting the filenames of all files in a folder

它还会重定向到另一个线程,其中的代码也不适合我。

我的应用程序不断崩溃,错误提示它是一个空指针。

我尝试检查目录

    File dir = new File("C:\\Users\\myName\\files");

    //Log.d("filePath:", filePath);

    if(dir.isDirectory()) {
       Log.d()
    }

但我的 if 语句中没有日志。显然它没有被当作一个目录。我一定忽略了一些非常简单的事情。

【问题讨论】:

  • C:\\Users\\myName\\files 不是您的 Android 设备/模拟器上的目录。也就是说,您的设备/模拟器没有 C: 驱动器。
  • 哇,我觉得自己很笨。谢谢你。我正在尝试允许用户输入目录,然后对其进行扫描。它必须在 android 中吗?一般会是什么样子?

标签: java android file path


【解决方案1】:

你可以使用这个 API

    Environment.getExternalStorageDirectory()// /storage/sdcard0

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

如果您想获取详细信息,请访问以下链接

https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

https://gist.github.com/granoeste/5574148

【讨论】:

  • 如果用户想在设备上或设备外指定目录,会是什么样子?
  • 我希望用户能够输入目录名称并让我的应用程序在该文件夹中找到文件。如果我需要在设备中执行此操作,目录示例应该是什么样的?
  • 确实有点。我只是对路径名在模拟器中的样子感到困惑。我真的不知道根在哪里以及我应该如何调用它及其子文件夹。
  • 给我一个源代码链接(github)。所以我可以帮助你。
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 2012-01-18
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多