Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getFilesDir() vs Environment.getDataDirectory() [duplicate]

Tags:

android

I want to replace one context method with Environment variable.
But not sure if they mean the same.

What is the difference between

getFilesDir() and Environment.getDataDirectory() ?

How can I get data\data as a from of data\data\com.Myapp using Environment Variable?

like image 930
Elad Benda2 Avatar asked Jan 20 '14 09:01

Elad Benda2


2 Answers

From the HERE and HERE

public File getFilesDir ()

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

public static File getExternalStorageDirectory ()

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

If you want to get your application path use getFilesDir() which will give you path /data/data/your package/files

You can get the path using the Environment var of your data/package using the

getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath(); which will return the path from the root directory of your external storage as /storage/sdcard/Android/data/your pacakge/files/data

To access the external resources you have to provide the permission of WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE in your manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

Check out the Best Documentation to get the paths of direcorty

like image 127
GrIsHu Avatar answered Oct 12 '22 03:10

GrIsHu


Environment returns user data directory. And getFilesDir returns application data directory.

like image 28
Adam Radomski Avatar answered Oct 12 '22 01:10

Adam Radomski