1

So I would like to save some data on external storage on android. And I have looked at the the api where is says getExternalStoragePublicDirectory(String type) "This method will create the appropriate directory if necessary." These are the default directories: Music,Podcasts,Ringtones,Alarms,Notifications,Pictures,Movies,Download. So my question is:

  1. How do I create a new directory of my own?
  2. How can I make it invisible to the user? so that user will not get frustrated with the extra folder.
sammiwei
  • 3,140
  • 9
  • 41
  • 53

2 Answers2

7

You could do something like this, get your application directory

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

Now this creates a custom directory on external storage as the android docs 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.

EDIT:

Use Context.getDir(String name, int mode) method to create or access directories in internal storage. Quote from docs:

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

Example:

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir, set it to private so its only visible to our application;
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • I do want to save files that are not specific to applications should not be deleted the app is uninstalled, but the things is, how would I make it invisible for the user, so that the user wont delete it by accident. – sammiwei Jan 17 '12 at 01:13
  • 1
    The folder i created in the code is visible to the user..You can name it what ever you want in the place of "AppName" all Enviroment.getExternalStorageDirectory() does is gets the path to external storage. – coder_For_Life22 Jan 17 '12 at 01:18
  • I see, but the folder is not visible to the user if they do not access the code, right? – sammiwei Jan 17 '12 at 02:11
  • emmmmm I guess I am trying to hit 3 birds with one stone...I want it to be cross app accisiable, and stays in the storage when my own app even gets uninstalled, and invisible to the user....Let me see if I can find an alternative way. But, million thanks to you!! – sammiwei Jan 17 '12 at 02:36
6

You can use coder_For_Life22's answer about creating the folders, and then to make it "more-invisible" to the users, you can do the following:

  1. Name it starting with dot. e.g. ".yourappname"
  2. Put an empty file ".nomedia" into your folder. This prevents the media scanner from recording the files you have on that folder.
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228