Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private folder on Internal/external storage on android

Tags:

file

android

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;
    }
like image 365
pats Avatar asked Oct 20 '15 09:10

pats


1 Answers

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.

like image 200
Rahul Tiwari Avatar answered Sep 27 '22 17:09

Rahul Tiwari