1

I am trying to create a app specific directory (Internal) to store some images.I have tried many SO answers, but still nothing works. I've tried this from here.

File mydir = getApplicationContext.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;

But still this directory is not created - Android/data/com.packagename/....

But when I run this-

File mediaDir = new File("/sdcard/Android/data/com.packagename");
if (!mediaDir.exists()){
    mediaDir.mkdir();
}

This though creates the directory in the internal storage but is this the right way to do it ?

  • And yes I've added the internal read write permissions
Community
  • 1
  • 1
shadyinside
  • 630
  • 1
  • 8
  • 23
  • In addition to the misconceptions already noted, you need to know that the first option with internal storage will be tricky to verify using anything other than the app itself (or on a rooted device such as an emulator) as the files would typically be private to the app and not visible to adb, ddms, or a connected PC when run on a secured device. – Chris Stratton Feb 26 '14 at 19:34
  • but the whole directory hierarchy can be extracted using `adb backup -f file-backup.ab com.packagename.app` – Zennichimaro May 20 '14 at 08:20

2 Answers2

3

getApplicationContext().getDir() will not create a folder named Android/data/com.packagename/. If you log/print the path for the returned File you will see it is "/data/data/<packagename>/app_mydir/". This is the way it should be for internal storage.

If you want to create a directory on the sd card (usually referred to as external storage, even when the /sdcard path resides in internal flash storage) then use Context.getExternalFilesDir(). This will create a folder like "/sdcard/Android/data/<packagename>/files/"

marcone
  • 321
  • 1
  • 5
  • Will '"/sdcard/Android/data//files/"' be directly visible to the user? I mean if I store some images, it won't show up in the gallery , right ? – shadyinside Feb 26 '14 at 19:03
  • No, they won't show up in Gallery. But if that's the location you want to use, don't hardcode it in your app, use getExternalFilesdir() instead. – marcone Feb 26 '14 at 19:08
  • Ok. Actually i thought external storage refers to SD Card. Anyways Thanks. – shadyinside Feb 26 '14 at 19:14
  • Why do you say those external storage files won't show up in Gallery? I'd expect that they would, as soon as the media scanner sees them (on the next boot, if not sooner as a result of invoking it on the folder) – Chris Stratton Feb 26 '14 at 19:30
  • Because the system creates a .nomedia file in /sdcard/Android/data/ – marcone Feb 26 '14 at 21:42
  • I used context.getExternalFilesDir(), it got created folders in internal storage and external storage.And file got created in internal storage.Then what is the limit for internal storage.like 1MB in cache, or how much – Hanuman Dec 09 '16 at 08:22
0

Internal Storage:
To find locations on internal storage for your app, use getFilesDir(), called on any Context (such as your Activity, to get a File object.

There's getDir,
http://developer.android.com/reference/android/content/Context.html#getDir(java.lang.String,int)

Please have a look at:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

For your purpose, instead of what you did with the path of sdCard, you can use

getExternalFilesDir

http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

It returns the path to files folder inside Android/data/data/your_package/ on your SD card. It is used to store any required files for your app (e.g. images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.
Also, Starting in KITKAT, no permissions are required to read or write to the returned path; it's always accessible to the calling app. This only applies to paths generated for package name of the calling application. To access paths belonging to other packages, WRITE_EXTERNAL_STORAGE and/or READ_EXTERNAL_STORAGE are required.

Can check more:
http://www.mysamplecode.com/2012/06/android-internal-external-storage.html

http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html

For API 8 and above that would work fine.

In case you want to a similar implementation for API 7 and below also,

getExternalStorageDirectory()

It returns the root path to your SD card (e.g mnt/sdcard/). If you save data on this path and uninstall the app, that data won't be lost.

Pararth
  • 8,114
  • 4
  • 34
  • 51
  • I want it on the internal storage. The directory which was successfully created was on the **internal**. For external if i change it to sdcard1, it then stores to the SD Card. But i want for internal. – shadyinside Feb 26 '14 at 18:59
  • if you want internal storage, then you should use Context.getDir(), and don't worry about what the exact path is. Just use what that method returns. I suspect you may have a different notion of what "internal" means than I do, though. It might help if you explained what exactly you want to use the storage for. – marcone Feb 26 '14 at 19:07