Android - creating custom directory to store files
In my app, I am intending to sto开发者_如何学Cre 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.
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.
@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();
精彩评论