Getfilesdir() VS Environment.Getdatadirectory()

getFilesDir() vs Environment.getDataDirectory()

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

how to access getFilesDir() as an environment variable?

It's not possible.

Context.getFilesDir() returns a path that is bound to your package and the Context is required to access the package name.

Environment is different as there's only constants that are common to all apps running on the same runtime.

However, a Context is available practically everywhere in an Android application so this shouldn't really be a problem.


Follow up:

how about Environment.getDataDirectory()? how can I get data\data as from data\data\com.Myapp using EnvironmentVar?

Environment.getDataDirectory() just returns the part of the data directory common to all apps. For example, the /data/data.

To get your own files dir (getFilesDir()), your package name and "/files" need to be appended to it. Context implementation does this for you.

Difference between getExternalStorageDirectory() and getDataDirectory()

I am referring to the Storage where generally files are saved such as WhatsApp Folder, DCIM, Download etc.

That is what the Android SDK refers to as external storage.

The above method is working in most of the device but some device this method is giving issue that is it is returning the SD Card path.

It should return the location of external storage on 100% of Android devices. Whether external storage is removable or not is up to the device manufacturer. The vast majority of Android devices have permanent non-removable external storage, typically on the same partition as internal storage in the on-board flash memory of the device.

Someone who could tell me how to exactly get the Internal storage path in those device whose Environment.getExternalStorageDirectory().getPath() return External Storage path (SD Card path).

Most likely, there is no such path.

the Environment.getExternalStorageDirectory().getPath() for the same device returns /storage/sdcard0 which points to Internal Storage when SD Card is not present but when an SD Card is present it gives the path which points to SD Card and not Internal Storage.

The value of getExternalStorageDirectory() should not vary based on the presence or absence of some piece of removable media. If your description is accurate, then that is a buggy device, and there is little that you can do about it. Certainly, there is no standard means of getting the "other" getExternalStorageDirectory() value, since that value should not be changing.

(BTW, what device do you have that behaves this way?)

Can anyone help me understand the difference between the above two

getDataDirectory() more or less returns the root of internal storage. I say "more or less" as apps never really work with this directory, but instead with app-specific subdirectories (e.g., getFilesDir() on Context).

How I get android/data/package name/

Though this is not the write way of getting the package name from your data folder but this can be used as a temporary solution:

File file=getApplicationContext().getFilesDir();

Thanks to @blackapps for making the solution more simple.

How do I access the textfile I created?

File file = new File(Environment.getExternalStorageDirectory()+"/Download/", "yourFile.extension");

This is for downloads directory.

Then to open your file

Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(file));
Intent j = Intent.createChooser(myIntent, "Choose an application to open with:");
startActivity(j);


Related Topics



Leave a reply



Submit