获取/data/data/my.app/files路径​​编程?路径、my、data、files

2023-09-06 09:31:16 作者:花间冢

在我的应用程序,我刚刚成功下载一个文件,我打开了它是这样的:

In my app, I've just successfully downloaded a file and I opened it like this:

OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);

随着一点点的浏览,我发现我的文件中的 /data/data/my.app.namespace/files/myfile.txt 。我不想硬code中的路径,但后来在我的应用我将要访问的myfile.txt,我要动态地获取文件夹路径。我试过最喜欢 Environment.getDataDirectory()在环保对象中的文件夹。getAbsolutePath()但只返回 /数据。我在哪里可以期待,以到达私人文件去了?或者是安全的假设,这个结构会不会改变?我不喜欢硬编码,但它会工作......

With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...

推荐答案

您可以用做 context.getFilesDir()+/filename.ext\"

 
精彩推荐