36

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

1.使用 getDir

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

2.使用getFilesDir

File dir = getFilesDir();

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

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

4

1 回答 1

43

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.

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.

于 2012-08-02T17:42:30.643 回答