Android.Content.Context.GetExternalFilesDir Method
Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Android.OS.Environment.ExternalStorageDirectory) where the application can place persistent files it owns.

Syntax

[Android.Runtime.Register("getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;", "GetGetExternalFilesDir_Ljava_lang_String_Handler")]
public abstract Java.IO.File GetExternalFilesDir (string type)

See Also

Context.FilesDir
Android.OS.Environment.GetExternalStoragePublicDirectory(string)

Parameters

type
The type of files directory to return. May be null for the root of the files directory or one of the following Environment constants for a subdirectory: Android.OS.Environment.DirectoryMusic, Android.OS.Environment.DirectoryPodcasts, Android.OS.Environment.DirectoryRingtones, Android.OS.Environment.DirectoryAlarms, Android.OS.Environment.DirectoryNotifications, Android.OS.Environment.DirectoryPictures, or Android.OS.Environment.DirectoryMovies.

Returns

Documentation for this section has not yet been entered.

Remarks

Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Android.OS.Environment.ExternalStorageDirectory) where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.

This is like Context.FilesDir in that these files will be deleted when the application is uninstalled, however there are some important differences:

Starting in NoType:android/os/Build$VERSION_CODES;Href=../../../reference/android/os/Build.VERSION_CODES.html#KITKAT, no permissions are required to read or write to the returned path; it's always accessible to the calling app. This only applies to paths generated for package name of the calling application. To access paths belonging to other packages, NoType:android/Manifest$permission;Href=../../../reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE and/or NoType:android/Manifest$permission;Href=../../../reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE are required.

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

Here is an example of typical code to manipulate a file in an application's private storage:

java Example

void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        file.delete();
    }
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        return file.exists();
    }
    return false;
}

If you supply a non-null type to this function, the returned file will be a path to a sub-directory of the given type. Though these files are not automatically scanned by the media scanner, you can explicitly add them to the media database with Android.Media.MediaScannerConnection.ScanFile(Context, System.String[], System.String[], System.String[]). Note that this is not the same as Android.OS.Environment.GetExternalStoragePublicDirectory(string), which provides directories of media shared by all applications. The directories returned here are owned by the application, and their contents will be removed when the application is uninstalled. Unlike Android.OS.Environment.GetExternalStoragePublicDirectory(string), the directory returned here will be automatically created for you.

java Example

void createExternalStoragePrivatePicture() {
    // Create a path where we will place our picture in our own private
    // pictures directory.  Note that we don't really need to place a
    // picture in DIRECTORY_PICTURES, since the media scanner will see
    // all media in these directories; this may be useful with other
    // media types such as DIRECTORY_MUSIC however to help it classify
    // your media for display to the user.
    File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(this,
                new String[] { file.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivatePicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory and delete the file.  If external
    // storage is not currently mounted this will fail.
    File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (path != null) {
        File file = new File(path, "DemoPicture.jpg");
        file.delete();
    }
}

boolean hasExternalStoragePrivatePicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory and check if the file exists.  If
    // external storage is not currently mounted this will think the
    // picture doesn't exist.
    File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    if (path != null) {
        File file = new File(path, "DemoPicture.jpg");
        return file.exists();
    }
    return false;
}

Here is an example of typical code to manipulate a picture in an application's private storage and add it to the media database:

[Android Documentation]

Requirements

Namespace: Android.Content
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 8