-1

I'm trying to make a folder in internal storage (where the user can access the folder) in Android Studio (if the folder is created, I don't want to create it again).

I've seen that link: Android create folders in Internal Memory, but here it doesn't shows what I want. I want the data to be accessible for the user, I don't want to store it here /data/data/package.name/app_MyDirName, for example.

I've tried that code but there is always a problem running it:

    public void createFolder(View v){
        File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "YOUR_FOLDER_NAME_HERE");
        if (!folder.exists()) {
            if(folder.mkdirs()){
                Log.d("App","file created successfully");
            }else{
                Log.d("App","error creating folder");
            }
        }else{
            Log.d("App","folder exists");
        }
    }

Here it's the output of the logcat:

2020-12-26 17:56:17.433 5981-5981/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-12-26 17:56:17.434 5981-5981/? E/Zygote: accessInfo : 1
2020-12-26 17:56:17.508 5981-5981/? E/mple.pruebadoc: Unknown bits set in runtime_flags: 0x8000
2020-12-26 17:56:18.878 5981-9037/com.example.pruebadocs E/gralloc: Arm Module v1.0
2020-12-26 17:56:18.879 5981-9037/com.example.pruebadocs E/ion: ioctl c0044901 failed with code -1: Invalid argument

I always receive the "error creating folder message", but I don't have any idea why.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

take a look at the answer at android-create-folders-in-internal-memory. I think they made a good example, if you are looking for a solution in java.

EDIT: As pointed out by someone, i post the full example for easier understanding:

File folder = new File(Environment.getDataDirectory() + File.separator + "YOUR_FOLDER_NAME_HERE");
if (!folder.exists()) {
    if(!folder.mkdirs()){
        //DO SOME ERROR HANDLING HERE
}
}
  • I've tried that code but there is always an error creating the file. Does anyone know what can be the issue? – Samuel Molina Perales Dec 26 '20 at 16:45
  • @SamuelCoaster what is the error you are getting? Maybe we can find a solution if you post it here – Alexander Klement Dec 26 '20 at 16:52
  • I've put it in the question – Samuel Molina Perales Dec 26 '20 at 16:59
  • Ou ok, i did not see that, the call "folder.mkdirs()" does only return true if the folder realy was created, see: https://developer.android.com/reference/java/io/File#mkdirs(), so if the folder does already exist, it should return false. I would suggest to change the name of the folder if you haven't already tried. Maybe you get a true then. Do you have the possibility to check if the folder exists? – Alexander Klement Dec 26 '20 at 17:08
0

It will check if (mydir) my directory is exist or not if not exist (mydir.mkdirs()) will create it

File mydir = context.getDir("users", Context.MODE_PRIVATE);    
if (!mydir.exists()) {
      mydir.mkdirs(); 
}
Shahryar Ahmed
  • 197
  • 1
  • 9