我也没有意义Context.getFileStreamPath试图测试阉文件路径是否存在? [英] I can't make sense Context.getFileStreamPath trying to test wether a file path exists?

查看:1403
本文介绍了我也没有意义Context.getFileStreamPath试图测试阉文件路径是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让getFileStreamPath疯狂的感觉了几个小时了。
可能有人请解释如何测试一个路径=店/箱/水果的存在?
在试图简化测试我已经打破到细分的路径。
我以为我受够了。但是,当存在铺测试中断,但没有crates..Weird!
或者是什么?

 公共静态布尔pathExists(字符串路径,上下文CTX)
{
    布尔结果= FALSE;
    的String [] = pathSegments path.split(/);
    串pathStr =;
    的for(int i = 0; I< pathSegments.length;我++)
    {
        pathStr + = pathSegments [I]
        如果(!ctx.getFileStreamPath(pathStr).exists())
        {
            结果= FALSE;
            打破;
        }
        pathStr + =/;
        结果=真;
    }
    返回结果;
}


解决方案

是的,蹩脚的Andr​​oid的API。
秘决是使用context.getFilesDir()。这就是你的文件:

 文件fil​​e =新的文件(context.getFilesDir()+文件分割符+路径);

在此之后,该文件可以正常工作

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

解决方案

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.

这篇关于我也没有意义Context.getFileStreamPath试图测试阉文件路径是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆