【问题标题】:I can't make sense Context.getFileStreamPath trying to test wether a file path exists?我无法理解 Context.get FileStream Path 试图测试文件路径是否存在?
【发布时间】:2012-03-05 10:05:08
【问题描述】:

我已经尝试理解 getFileStreamPath 的疯狂已经有几个小时了。 有人可以解释一下如何测试 path = "shop/crates/fruits" 是否存在吗? 为了简化测试,我打破了分段的路径。 我以为我有它。但是当商店存在但没有板条箱时,测试就会中断..奇怪! 还是这样?

public static Boolean pathExists(String path, Context ctx)
{
    Boolean result = false;
    String[] pathSegments = path.split("/");
    String pathStr = "";
    for(int i = 0;i<pathSegments.length;i++ )
    {
        pathStr += pathSegments[i];
        if(!ctx.getFileStreamPath(pathStr).exists())
        {
            result = false;
            break;
        }
        pathStr += "/";
        result = true;
    }
    return result;
}

【问题讨论】:

    标签: android android-file


    【解决方案1】:

    是的,糟糕的 android API。 秘诀是使用 context.getFilesDir()。那是你的文件:

    File file = new File(context.getFilesDir() + File.separator + path);
    

    之后,文件正常运行。

    【讨论】:

      【解决方案2】:

      首先,getFileStreamPath 返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的绝对路径。

      您是要测试内部存储还是外部存储的路径?如果您想使用外部存储,请使用getExternalFilesDir()。如果你想使用像res/raw这样的内部包嵌入资源,那就另当别论了:

      Android how to get access to raw resources that i put in res folder?

      但我认为它不会像你假设的那样起作用。

      阅读http://developer.android.com/guide/topics/data/data-storage.html 小心。

      阅读Using the Internal Storage 那里的章节。

      另见:

      How to create a file on Android Internal Storage?

      Read/write file to internal private storage

      http://www.vogella.de/articles/AndroidFileSystem/article.html

      【讨论】:

      • 你似乎不明白它是如何工作的。首先,getFileStreamPath 返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的绝对路径。 我完全同意。使用文件和目录总是有点难看。 Android 文件系统比一般的文件系统对象更丑。当然,您必须使用 RTFM,然后就可以了。除非你错过了最后期限。我知道我并不孤单。有很多沮丧的人试图破解它。太糟糕了..对我来说看起来像是一个彻底的重新设计。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2019-03-02
      • 2012-06-29
      • 2011-06-14
      • 1970-01-01
      相关资源
      最近更新 更多