Android 上的 getDir 和 getFilesDir 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11781260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:50:48  来源:igfitidea点击:

What is the difference between getDir and getFilesDir on Android?

androidfileprivate

提问by cleroo

I would like to save a picture in Internal Storage of my app, to make it private. So I did a little research and saw 2 ways to get the directory.

我想在我的应用程序的内部存储中保存一张图片,以使其私密。所以我做了一些研究,看到了两种获取目录的方法。

1.With getDir:

1.使用getDir

File dir = getDir(Environment.DIRECTORY_PICTURES, Context.MODE_PRIVATE);

2.With getFilesDir:

2.使用getFilesDir

File dir = getFilesDir();

Which is best ? Which returns the location in Internal Storage ?

哪个最好?哪个返回内部存储中的位置?

The way of writing files depends on the way you get your dir ? I am a bit lost since there are many ways to write files in Android.

写入文件的方式取决于您获取目录的方式?我有点迷茫,因为在 Android 中有很多方法可以编写文件。

回答by Erol

getFilesDir()returns a File object to a directory that is private to your application only. When you use openFileOutput(String, int), the data you write is directly stored in this directory and is not accessible by any other application. It holds your application files.

getFilesDir()将 File 对象返回到仅对您的应用程序私有的目录。当您使用 时openFileOutput(String, int),您写入的数据将直接存储在此目录中,任何其他应用程序都无法访问。它保存您的应用程序文件。

getDir()enables you to create any file or directory in the internal memory, which is also accessible by other applications depending on the mode you create it. openFileOutput(String, int)will not work with the output of this so you will have to use other means of writing and reading files to deal with this directory or file. This is more used for creating custom files other than files only used by your application.

getDir()使您能够在内部存储器中创建任何文件或目录,其他应用程序也可以根据您创建的模式访问这些文件或目录。openFileOutput(String, int)将无法使用此输出,因此您将不得不使用其他写入和读取文件的方式来处理此目录或文件。这更多地用于创建自定义文件,而不是仅由您的应用程序使用的文件。