Search code examples
androidmemoryinternal

Android create folders in Internal memory issue


I have a little issue with creating folders for my application in Internal Memory. I'm using this piece of code :

    public static void createFoldersInInternalStorage(Context context){
    try {

        File usersFolder = context.getDir("users", Context.MODE_PRIVATE);
        File fileWithinMyDir = new File(usersFolder, "users.txt"); //Getting a file within the dir.
        FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

        File dataFolder = context.getDir("data", Context.MODE_PRIVATE);
        File fileWithinMyDir2 = new File(dataFolder, "data.txt"); //Getting a file within the dir.
        FileOutputStream out2 = new FileOutputStream(fileWithinMyDir2); //Use the stream as usual to write into the file.

        File publicFolder = context.getDir("public", Context.MODE_PRIVATE);
        File fileWithinMyDir3 = new File(publicFolder, "public.txt"); //Getting a file within the dir.
        FileOutputStream out3 = new FileOutputStream(fileWithinMyDir3); //Use the stream as usual to write into the file.

    } catch(FileNotFoundException e){
        e.printStackTrace();
    }
}

So folders are created but in front of their name there is the beginning "app_" : app_users, app_data, app_public. Is there a way to create the folders with the name given by me? And another question : I want to first create folder Documents and than all other folders "Data, Public, Users" on it.... And the last question : How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?

Thanks in advance!


Solution

  • You can use this :

    File myDir = context.getFilesDir();
    String filename = "documents/users/userId/imagename.png";
    File file = new File(myDir, filename);
    file.createNewFile();
    file.mkdirs();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(mediaCardBuffer);
    fos.flush();
    fos.close();