10

I am unable to get the methods for creating folder in Internal Memory,

i gone through few conversations in Android create folders in Internal Memory and Problem facing in reading file from Internal memory of android. But still i am unable to meet my requirement.

My requirement is , I want to create a folder in Internal Memory, there i want to Store one video.

Thankyou you very much in advance for valuable feedbacks.

Community
  • 1
  • 1
Naveen
  • 371
  • 3
  • 4
  • 14

6 Answers6

24

try the below

File mydir = context.getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
     mydir.mkdirs();
}     
Yojimbo
  • 23,288
  • 5
  • 44
  • 48
9

Here is the code which I am using for creating files in internal memory :

    File myDir = context.getFilesDir();
    // Documents Path
    String documents = "documents/data";
    File documentsFolder = new File(myDir, documents);
    documentsFolder.mkdirs(); // this line creates data folder at documents directory

    String publicC = "documents/public/api." + server;
    File publicFolder = new File(myDir, publicC);
    publicFolder.mkdirs(); // and this line creates public/api.myservername folder in internal memory
hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • You have no clue how long I have been looking for this. Every time I would try it it would create the file outside of the files directory and it would append "app_" to the directory. +1 From me for the excelent, small and practical solution. – sigillum_conf Mar 06 '17 at 19:06
  • This creates the folder in internal memory but also in application package. – user3099225 Jun 19 '19 at 18:32
5

To create directory on phone primary storage memory (generally internal memory) you should use following code. Please note that ExternalStorage in Environment.getExternalStorageDirectory() does not necessarily refers to sdcard, it returns phone primary storage memory

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyDirName");

if (!mediaStorageDir.exists()) {
    if (!mediaStorageDir.mkdirs()) {
        Log.d("App", "failed to create directory");
        return null;
    }
}

Directory created using this code will be visible to phone user. The other method (as in accepted answer) creates directory in location (/data/data/package.name/app_MyDirName), hence normal phone user will not be able to access it easily and so you should not use it to store video/photo etc.

You will need permissions, in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
Tone
  • 1,701
  • 1
  • 17
  • 18
prodev
  • 575
  • 5
  • 15
  • 9
    **FALSE AND MISLEADING** - there is no such permission as android.permission.WRITE_INTERNAL_STORAGE – Chris Stratton Jan 24 '18 at 05:50
  • this answer is from May 2016, kindly check this permission with android version used that time. Android version changes so do their permissions. It will work in Android 5.0 – prodev Jan 24 '18 at 06:06
  • 5
    **FALSE AGAIN**. There has **never** been such an **internal** storage permission - to even suggest it is to betray a deep and fundamental misunderstanding of android filesystems. The actual fact is that apps always have permission to **their own** internal storage folder. – Chris Stratton Jan 24 '18 at 07:10
2
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
File direct = new File(Environment.getExternalStorageDirectory()+"/folder_name");

if(!direct.exists()) {
     if(direct.mkdir()); //directory is created;
}
Sujeet Kumar
  • 1,822
  • 22
  • 25
1

There is a "cacheDirectory" in your "data/package_name" directory.

If you want to store something in that cache memory,

File cacheDir = new File(this.getCacheDir(), "temp");
if (!cacheDir.exists())
    cacheDir.mkdir();

where this is context.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0
try {
    File cashDir = new File(dir.getCanonicalPath(),"folder");
    if(!(cashDir.exists())) cashDir.mkdirs();
} catch (IOException e) {
    e.printStackTrace();
}
  
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Helen
  • 1
  • 1
  • Please add some kind of explanation to your solution to OPs issue. One cannot make out what this yields – shuberman Jul 12 '20 at 08:15