I am trying to create a folder in the phone's internal memory (not in the RAM
, on the disk where the files are persisted) Android
.
Note: I am programming in Java
.
package com.example.liferegisterdiary;
/*
*
* This is a file and folder controller 2021
* Here you create and read files and folders.
*
* */
import android.os.Environment;
import java.io.File;
public class FileFolderController {
public boolean mkdirStatus;
public FileFolderController() {
mkdirStatus = true;
makeDir("ejemplo");
}
public void makeDir(String folderR) {
try {
File newFolder = new File(Environment.getDataDirectory(), folderR);
mkdirStatus = mkdirStatus & newFolder.mkdirs();
} catch (Exception e) {
mkdirStatus = mkdirStatus & false;
}
}
public String health() {
return "Estoy en el controlador de archivos y carpetas...." + mkdirStatus;
}
}
What I need to do is create a folder in the root
main internal memory of the phone, Note: I don't microSD
need it in the internal memory... The problem is that it doesn't even let me create a folder in /data...
Somebody could help me????
Note: I am going to leave an example Python
of how to create a folder in Windows
:
import os
from os import scandir
if not os.path.isdir(self.rutaDelProyecto+"\\DATA"): # No existe la carpera creelas
os.mkdir(self.rutaDelProyecto+"\\DATA")
This program is very simple: it only checks if the data folder exists and if it doesn't, it creates that folder... What I want to achieve Android
is the same... as creating a folder... what happens is that I've seen many tutorials and tried a thousand things and I can't create a folder, please help.
To do this you have to import the context from the main_activity
// Main Activity
// Then in the folder controller you have to invoke the route
And with this it will be created in /MemoriaInterna/Android/data/com.nombreAPP/files/FolderQueSeDesireCrear
If you want to create a file or directory in internal memory you should use getFilesDir() :
If you want to do this in another file that is not the one,
Activity
you must send the context as a parametergetExternalFilesDir() is used to create a file or directory on external storage as it returns the absolute path to the directory on the main external/shared storage device where the application can put persistent files it owns.