Parameters

Parameter

Details

name

The name of the file to open. NOTE: Cannot contain path separators

mode

Operating mode. Use MODE_PRIVATE for default operation, and MODE_APPEND to append an existing file. Other modes include MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, which were both deprecated in API 17.

dir

Directory of the file to create a new file in

path

Path to specify the location of the new file

type

Type of files directory to retrieve. Can be null, or any of the following: DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES

Android: Internal and External Storage - Terminology Clarification

Android developers(mainly beginners) have been confused regarding Internal & External storage terminology. There are lot of questions on Stackoverflow regarding the same. This is mainly because of the fact that terminology according to Google/official Android documentation is quite different to that of normal Android OS user. Hence I thought documenting this would help.

This type of storage can be accessed on windows PC by connecting your phone to PC via USB cable and selecting Camera(PTP) in the USB options notification.

This type of storage can be accessed on windows PC by connecting your phone to PC via USB cable and selecting File transfer in the USB options notification.

In a nutshell,

External Storage(GT) = Internal Storage(UT) and External Storage(UT)

Removable Storage(GT) = External Storage(UT)

Internal Storage(GT) doesn't have a term in UT.

Let me explain clearly,

Internal Storage(GT): By default, files saved to the internal storage are private to your application and other applications cannot access them. Your app user also can't access them using file manager; even after enabling "show hidden files" option in file manager. To access files in Internal Storage(GT), you have to root your Android phone. Moreover, when the user uninstalls your application, these files are removed/deleted.

So Internal Storage(GT) is NOT what we think as Nexus 6P's 32/64 GB internal memory

Every Android-compatible device supports a shared "external storage" that you can use to save files. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

External Storage(GT) location: It could be anywhere in your internal storage(UT) or in your removable storage(GT) i.e. micro SD card. It depends on your phone's OEM and also on Android OS version.

In order to read or write files on the External Storage(GT), your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions.

If you need to both read and write files, then you need to request
only the WRITE_EXTERNAL_STORAGE permission, because it implicitly
requires read access as well.

In External Storage(GT), you may also save files that are app-private

But,

When the user uninstalls your application, this directory and all its contents are deleted.

When do you need to save files that are app-private in External Storage(GT)?

If you are handling files that are not intended for other apps to use (such as graphic textures or sound effects used by only your app), you should use a private storage directory on the external storage

Beginning with Android 4.4, reading or writing files in your app's private directories does not require the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions. So you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:

It returns absolute paths to application-specific directories on all
shared/external storage devices where the application can place
persistent files it owns. These files are internal to the application,
and not typically visible to the user as media.

That means, it will return paths to both types of External Storage(GT) - Internal memory and Micro SD card. Generally second path would be storage path of micro SD card(but not always). So you need to check it out by executing the code with this method.

Example with code snippet:

I created a new android project with empty activity, wrote the following code inside

Note: I have connected my phone to Windows PC; enabled both developer options, USB debugging and then ran this code. If you do not connect your phone; but instead run this on Android emulator, your output may vary. My phone model is Coolpad Note 3 - running on Android 5.1

Storage locations on my phone:

Micro SD storage location: /storage/sdcard1

Internal Storage(UT) location: /storage/sdcard0.

Note that /sdcard & /storage/emulated/0 also point to Internal Storage(UT). But these are symlinks to /storage/sdcard0.

To clearly understand different storage paths in Android, Please go through this answer

Disclaimer: All the storage paths mentioned above are paths on my phone. Your files may not be stored on same storage paths. Because, the storage locations/paths may vary on other mobile phones depending on your vendor, manufacturer and different versions of Android OS.

Before attempting to write or read from External Storage, you should always check that the storage medium is available.

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// Available to read and write
}
if (state.equals(Environment.MEDIA_MOUNTED) ||
state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
// Available to at least read
}

When writing files to the External Storage, you should decide if the file should be recognized as Public or Private. While both of these types of files are still accessible to the user and other applications on the device, there is a key distinction between them.

Public files should remain on the device when the user uninstalls the app. An example of a file that should be saved as Public would be photos that are taken through your application.

Private files should all be removed when the user uninstalls the app. These types of files would be app specific, and not be of use to the user or other applications. Ex. temporary files downloaded/used by your application.

Here's how to get access to the Documents directory for both Public and Private files.

Public

// Access your app's directory in the device's Public documents directory
File docs = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "YourAppDirectory");
// Make the directory if it does not yet exist
myDocs.mkdirs();

Using Internal Storage

By default, any files that you save to Internal Storage are private to your application. They cannot be accessed by other applications, nor the user under normal circumstances. These files are deleted when the user uninstalls the application.