Naveen
Naveen

Reputation: 381

Creating Folder in Internal memory

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.

Upvotes: 11

Views: 48068

Answers (6)

Helen
Helen

Reputation: 1

try {
    File cashDir = new File(dir.getCanonicalPath(),"folder");
    if(!(cashDir.exists())) cashDir.mkdirs();
} catch (IOException e) {
    e.printStackTrace();
}
  

Upvotes: 0

Sujeet Kumar
Sujeet Kumar

Reputation: 1942

<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;
}

Upvotes: 2

user2017760
user2017760

Reputation:

try the below

File mydir = context.getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
     mydir.mkdirs();
}     

Upvotes: 24

prodev
prodev

Reputation: 573

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" />

Upvotes: 5

Chintan Rathod
Chintan Rathod

Reputation: 26034

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.

Upvotes: 1

hardartcore
hardartcore

Reputation: 17037

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

Upvotes: 9

Related Questions