Android creates folders in internal memory

I have a little problem creating folders for my application in internal memory. I use 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(); } } 

This creates folders, but before their name begins "app_" : app_users , app_data , app_public . Is there any way to create folders with the name given by me? And another question: I want to first create the Documents folder and all the other "Data, Public, Users" folders on it .... And the last question: how can I specify the correct path to the folder if I want to create a file in Documents/Users/myfile.txt in internal memory?

Thanks in advance!

+4
source share
2 answers

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(); 
+2
source

Is there a way to create folders with the name that I specified?

Use getFilesDir() and the Java I / O file instead of getDir() .

How can I specify the correct path to the folder if I want to create a file in Documents / Users / myfile.txt in internal memory?

Use getFilesDir() and a Java I / O file, such as the File constructor, which takes File and a String to build the path.

+1
source

Source: https://habr.com/ru/post/1381286/


All Articles