Skip to content Skip to sidebar Skip to footer

How To Create A Directory On The Internal Storage Upon Installation Of The Android Application?

I want to have a single folder where I will put all the resources I'm going to need and I want it on the internal storage. Since I want this directory to be created only once, I fo

Solution 1:

use getCacheDir(). It returns the absolute path to the application specific cache directory on the filesystem. Then you can create your directory

FilemyDir=newFile(getCacheDir(), "folder");
myDir.mkdir();

Solution 2:

IF you would like to store on the external storage SD card instead, you need to have this

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In your AndroidManifest.xml file

Also this for working with the storage:

Filedirect=newFile(Environment.getExternalStorageDirectory()+"/folder");

if(!direct.exists()) {
     if(direct.mkdir()); //directory is created;
}

Solution 3:

First of all, make sure to read up on and understand the different storage options you have available for your application. Google has a very good article on the subject here:

http://developer.android.com/guide/topics/data/data-storage.html

The answer above from Andy mentions Environment.getExternalStorageDirectory() (API Level 7) for writing to an SD card or similar, but you should keep in mind that files written to that location will not get deleted when the user uninstalls the app. The documentation says:

"Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace"

Instead, if you target API Level 8 and above, you should use Context.getExternalCacheDir() as files in that location will get removed when the app is uninstalled. If you really want files to persist even after an uninstall you should use Context.getExternalFilesDir(). There's also getCacheDir() and getFilesDir() for working with files on the internal storage.

As was mentioned in the comments to your question you really should post the errors here for us to be able to help you.

PS I'm new to answering questions on SO so I don't have enough rep to post links to all of the functions mentioned in my answer and I do not seem able to comment on previous posts.

Solution 4:

Android studio Download video in specific folder:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />          

  Stringpath= Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
  FilemFolder=newFile(path);
  if (!mFolder.exists()) {
      mFolder.mkdir();
  }
  FileDirectory=newFile("/sdcard/kaizzan/");
  Directory.mkdirs();

Solution 5:

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Filenewfile=newFile(Environment.getExternalStorageDirectory()+"/specifyfoldername","nestedfoldername");
    direct.mkdir();
// it will create two folder in your internal storage first is specifyfoldername and another one inside the mentioned folder which is nestedfoldername
    }

Post a Comment for "How To Create A Directory On The Internal Storage Upon Installation Of The Android Application?"