Private folder on Internal/external storage on android

10,452

you are doing it wrong.

from the documentation you pointed:

If you want to save files that are private to your app, you can acquire the appropriate directory by calling getExternalFilesDir() and passing it a name indicating the type of directory you'd like. Each directory created this way is added to a parent directory that encapsulates all your app's external storage files, which the system deletes when the user uninstalls your app.

For example, here's a method you can use to create a directory for an individual photo album:

public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

If none of the pre-defined sub-directory names suit your files, you can instead call getExternalFilesDir() and pass null. This returns the root directory for your app's private directory on the external storage.

Remember that getExternalFilesDir() creates a directory inside a directory that is deleted when the user uninstalls your app. If the files you're saving should remain available after the user uninstalls your app—such as when your app is a camera and the user will want to keep the photos—you should instead use getExternalStoragePublicDirectory().

so basically you need to use getExternalFilesDirfunction in the example above to get it deleted when app in uninstalled.


according to documentation for getExternalFilesDir()

Parameters

type The type of files directory to return. May be null for the root of the files directory or one of the following constants for a subdirectory: DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES.

so apart from predefined types you can use null for root of the directory.

Share:
10,452
pats
Author by

pats

Updated on June 16, 2022

Comments

  • pats
    pats almost 2 years

    I want to create a private folder for my app data. This folder has to be deleted automatically when the app is removed from the device. According to http://developer.android.com/training/basics/data-storage/files.html, I have to create a private folder. How can I achieve this. My code below. Folder is not deleted when the app is removed.

     public static String getAppFilePath(Context context) {
    
            String path = "";
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                path = Environment.getExternalStorageDirectory()
                        .getAbsolutePath();
    
                path += "/myFolder";
    
            } else {
    
                ContextWrapper c = new ContextWrapper(context);
                path = c.getFilesDir().getPath();
            }
            File ret = new File(path);
            if (!ret.exists()) {
                ret.mkdirs();
            }
            return path;
        }
    
  • pats
    pats over 8 years
    Yes, I read that part in the reference, but what Environment. variable should I use? I have documents and couple of images.
  • Rahul Tiwari
    Rahul Tiwari over 8 years
    check updated answer, you can use null for root as there is no subdirectory for documents. for pictures use DIRECTORY_PICTURES
  • laim2003
    laim2003 about 5 years
    Why does the javadoc say the following: "There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files." It seems like any app with storage access that knows my package name could access these files?
  • Rahul Tiwari
    Rahul Tiwari about 5 years
    @laim2003 use getFilesDir() instead if security of files is your concern