【问题标题】:Is there any way to return file path like '/storage/emulated/0/Document/(list of pdf file)' with flutter?有没有办法用颤振返回像'/storage/emulated/0/Document/(pdf文件列表)'这样的文件路径?
【发布时间】:2022-01-17 12:44:39
【问题描述】:

我想从手机本地存储访问 pdf 文件列表并在屏幕上显示 pdf 缩略图。所以我决定使用ExternalPath.getExternalStoragePublicDirectory(ExternalPath.DIRECTORY_DOCUMENTS) 使用https://pub.dev/packages/external_path 包访问文件路径('/storage/emulated/0/Document/(pdf 文件列表)'),并使用ListView.builder 在屏幕上显示所有 pdf 文件。但问题是它只返回一个字符串,如 '/storage/emulated/0/Document' 缺少 '.pdf' 文件。我该怎么做?请帮我。在这个问题上卡住了 1 周。

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    getExternalStoragePublicDirectory 和 ExternalPath.DIRECTORY_DOCUMENTS 是 Documents 目录的返回文件。您可以从该目录文件中获取子列表。在这里您必须检查文件的扩展名,并且目录文件可能在 Documents 目录中可用,您可以在 File 对象上使用 isDirectory() 进行检查。您也可以忽略或获取该目录中的检查文件。

    看看我为自己使用的java代码示例:

    public void findFilesWithExtension(File sourceDir, String extension, List<File> resultFilesList, boolean checkInternalDir)
        {
            List<File> childFiles = Arrays.asList(sourceDir.listFiles());
            for (File child : childFiles)
            {
                if (child.isDirectory())
                {
                    if (checkInternalDir)
                        findFilesWithExtension(child, extension, resultFilesList, checkInternalDir);
                }
                else if (child.getName().endsWith(extension))
                {
                    resultFilesList.add(child);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 2018-03-08
      • 2022-01-16
      • 1970-01-01
      • 2022-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多