I can't make sense Context.getFileStreamPath trying to test wether a file path exists?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 17:37:57

问题


I have tried to make sense of the getFileStreamPath madness for some hours now. Could someone please explain how to test if a path = "shop/crates/fruits" exists? In an attempt to simplify the test i have broken the path in to segments. I thought i had it. But the test breaks when shop exists but there is no crates..Weird! Or is it?

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

回答1:


Yeah, the crappy android APIs. The secret sauce is to use context.getFilesDir(). That's your file:

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

After that, the file behaves normally.




回答2:


First, the getFileStreamPath returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

Are you trying to test the internal or external storage's path? If you want to use external storage use getExternalFilesDir(). If you want to use internal package embedded resources like res/raw, it's another story:

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

But I don't think it will work the way you are presuming to get it.

Read this http://developer.android.com/guide/topics/data/data-storage.html carefully.

Read Using the Internal Storage chapter there.

Also see:

How to create a file on Android Internal Storage?

Read/write file to internal private storage

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



来源:https://stackoverflow.com/questions/9270098/i-cant-make-sense-context-getfilestreampath-trying-to-test-wether-a-file-path-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!