Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

file - What is the difference between getDir and getFilesDir on Android?

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 :

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

2.With 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...