【问题标题】:Read internal files inside a particular folder in Android在 Android 中读取特定文件夹中的内部文件
【发布时间】:2012-07-10 03:11:08
【问题描述】:

当然,这不是一个很难回答的问题,但我在实施时遇到了一些麻烦。我的问题是:我需要读取(或列出)Android 内部存储中特定文件夹(例如“myFolder”,位于内部存储根目录中)内的文件。当然,这是可能的,但如何做到这一点?提前致谢!

【问题讨论】:

    标签: android file


    【解决方案1】:

    要读取设备内的文件,您可以执行以下操作:

    // gets the files in the directory
    File fileDirectory = new File(Environment.getDataDirectory()+"/YourDirectory/");
    // lists all the files into an array
    File[] dirFiles = fileDirectory.listFiles();
    
    if (dirFiles.length != 0) {
        // loops through the array of files, outputing the name to console
        for (int ii = 0; ii < dirFiles.length; ii++) {
            String fileOutput = dirFiles[ii].toString();
            System.out.println(fileOutput); 
        }
    }
    

    如果您想从 SD 卡中取出文件,只需将 getDataDirectory 更改为 getExternalStorageDirectory

    【讨论】:

    • 谢谢布莱恩。这个sn-p对我很有用!
    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 2014-01-22
    • 2017-11-29
    • 1970-01-01
    • 2022-12-23
    • 2022-12-21
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多