3

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;
}
DevNull
  • 763
  • 5
  • 17
  • 30

2 Answers2

2

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.

Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
0

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

Community
  • 1
  • 1
Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
  • 1
    _You don't seem to get how it works. First of all the getFileStreamPath returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored._ I totally agree. Working with files and directories is always a bit ugly. The Android file system is just uglier than your average file system object. Sure you have to RTFM and then you will be fine. Except if you miss that deadline. I know i'm not alone with this position. There is a lot of frustrated people out there trying to hack it. Too bad..Looks like a complete redesign to me. – DevNull Feb 14 '12 at 06:15