sonofel
sonofel

Reputation: 112

getFilesDir() vs getExternalFilesDir(), which one is more recommended?

As far as I've noticed, using getFilesDir() to store data (camera captures in my case) is somewhat temporary. I say somewhat because if I close and reopen the app, I'm able to access the directory and file list, but not the file itself.

Is my logic wrong or is that by design?
Out of the 2 methods, which is more recommended in:

  1. general
  2. my case (privacy is needed)

Upvotes: 3

Views: 5330

Answers (3)

HughHughTeotl
HughHughTeotl

Reputation: 5789

Update for the latest Android (currently Android 14)

In the old days getFilesDir() used to mean internal storage, and getExternalFilesDir() used to mean anything plugged in (usually an SD card).

These days phones don't have SD card slots, so the device emulates external storage by grabbing a slice of internal storage for the purpose. So the historic differences - that internal was faster and smaller, and external slower and larger - are generally no longer true.

In both cases apps get scoped storage - i.e. a directory they can access without permissions, which isn't accessible by any other app (though see below). This has been the case since Android 10 (but only enforced in Android 11).

Files stored under getFilesDir() and getExternalFilesDir() will be deleted when the app is uninstalled, but will persist until then. (If you're finding a file disappears before then, you have a different problem. I would suggest posting your code in a separate question).

The only major remaining difference between internal and external is that apps can get request a permission to read/write the entirety of external storage, including files it doesn't own. So don't use external for files that need to remain private to the app.

More information in this article: https://medium.com/@tdcolvin/demystifying-internal-vs-external-storage-in-modern-android-c9c31cb8eeec

Upvotes: 3

atla praveen
atla praveen

Reputation: 49

The main difference between the two is: getFilesDir() - This path will be hidden from the user. This is more secure approach. getExternalFilesDir() - Accessible by the user.

Received a good documentation on this which provides clear understanding: http://androidvogella.blogspot.com/2015/12/getfilesdir-vs-getexternalfilesdir-vs.html

Upvotes: 1

MustafaKhaled
MustafaKhaled

Reputation: 1489

Simply, getFilesDir() refers to internal storage that cannot be accessible outside your application. It's not explorable by the device as well.

getExternalFilesDir() refers to external storage, it's a type of App-specific storage. this can be explorable as you find its directory in

data/data/your package name

In general, you may use external storage as internal storage may be limited.

For privacy, if you need you to file security, use getFilesDir() as it's not accessible outside your application as I mentioned before.

Upvotes: 7

Related Questions