【问题标题】:Android list files in private app storageAndroid 列出私有应用存储中的文件
【发布时间】:2012-10-11 08:47:07
【问题描述】:

我正在开发 Android 应用,我需要保存和加载功能。

我想通过此功能将重要数据保存在私有应用存储中:

public static void save(Object file, String fileName, Context context)
        throws IOException, FileNotFoundException {
    FileOutputStream fos = context.openFileOutput(fileName,
            context.MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(file);
    oos.flush();
    oos.close();
    fos.close();
    System.out.println(TAG + ": File saved successful!");
}

我还编写了加载数据的方法。 这很有效,因为我知道文件的确切名称。

我现在需要动态保存文件,动态生成名称。 如何列出保存在昂贵的应用程序存储中的文件? 所以我可以加载准确的吗?

【问题讨论】:

    标签: android load save


    【解决方案1】:

    给你!

    File dir = getFilesDir(); 
    File[] subFiles = dir.listFiles();
    
    if (subFiles != null)
    {
        for (File file : subFiles)
        {
            // Here is each file
        }
    }
    

    【讨论】:

    • 它返回 0 个文件...我如何检查我保存的文件是否已保存? :) 我得到“文件保存成功日志”
    • 我已经检查过文件是否存在...并且我已成功将其加载回应用程序中。
    • 那是因为您将文件保存到与我的示例不同的目录。我编辑了我的答案。
    • 虽然它可能会返回文件本身的路径 - 您需要从该字符串获取目录路径
    • 我不确定 dir 路径是什么。你能从保存功能中弄清楚吗?我正在尝试 com.myapp.example/data
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2011-09-16
    • 2017-01-05
    • 1970-01-01
    • 2021-07-11
    • 2011-11-18
    相关资源
    最近更新 更多