jeffreyveon
jeffreyveon

Reputation: 13810

Android - creating custom directory to store files

In my app, I am intending to store user generated files in a custom directory (that I intend to create) within the user's SD card storage.

I am doing that using:

String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathToExternalStorage + "/" + "AppName");
// have the object build the directory structure, if needed.
appDirectory.mkdirs();

Would you recommend taking this approach? Will this folder and files persist if the user deletes my application?

Because, the Android documentation says:

If you want to save files that are not specific to your application and that should not be deleted when your application is uninstalled, save them to one of the public directories on the external storage. These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others.

I would need the files to persist even if the user uninstalls the app, but I did not wanted to clutter an existing folder my adding my files there - I thought creating a separate folder would be better. I am targetting API v9 and above.

Upvotes: 5

Views: 5631

Answers (2)

1ambharath
1ambharath

Reputation: 391

@jeffreyveon , your solution worked for me . Wish I could comment , but less reputation

String path = Environment.getExternalStorageDirectory().toString();
                File appDirectory = new File(path + "/" + "FolderName");  
                appDirectory.mkdirs();

Upvotes: 1

Michell Bak
Michell Bak

Reputation: 13242

Seems like a pretty good way to do it. I do it in a similar way, except I keep everything in the data folder, i.e. sdcard/data/{packagename}.

Update:

You shouldn't do it like that. Use Environment.getDataDirectory() instead.

Upvotes: 5

Related Questions