Android mkdirs() not working

14,904

Solution 1

You are trying to create a directory called myvideo.mp4.

mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                        + "/NewDirectory/myvideo.mp4");
mediaFile.mkdirs();

should be

File(Environment.getExternalStorageDirectory(), "NewDirectory");
mediaFile.mkdirs();

or better

mediaFile = new File(getExternalCacheDir(), "NewDirectory");
mediaFile.mkdirs();

Here you can find the documentation for getExternalCacheDir()

Be aware the from kitkat writing on the root of the sdcard is not allowed anymore.

Edit: the path to the file should be:

mediaFile = new File(getExternalCacheDir(), "NewDirectory");
File file = new File(mediaFile, "myvideo.mp4");
Uri videoUri = Uri.fromFile(file);

Solution 2

For create the directory use:

String rootDirectory = Environment.getExternalStorageDirectory().toString();
File myDir = new File(rootDirectory + "/NewDirectory");
myDir.mkdir();

You can save the file with:

recorder.setOutputFile(Environment.getExternalStorageDirectory().toString() + "/NewDirectory/" +fileName);

For find the video for share or reproduce:

File sdcard = Environment.getExternalStorageDirectory();
File directory = new File(sdcard.getAbsolutePath() + "/NewDirectory");
File video = new File(directory, fileName);
Share:
14,904
JTK
Author by

JTK

Updated on June 27, 2022

Comments

  • JTK
    JTK about 2 years

    I''m developing my first Android application and I'v run into a problem while trying to create a directory to save recorded video files.

    I have a method in my main activity buttonOnClickRecord that invokes an intent to use the android camera, I'm also creating a file during this method call and I'm calling the mkdirs() method on it to create the directory to store the file.

    I have also implemented <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in my Manifest.

    public void buttonOnClickRecord(View v){
            mediaFile =
                    new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/NewDirectory/myvideo.mp4");
            mediaFile.mkdirs();
    
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
    
                Uri videoUri = Uri.fromFile(mediaFile);
                takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }
        }
    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
    
                Toast.makeText(this, "Video saved to:\n" +
                        data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
    
    
      Toast.makeText(this, "Video recording cancelled.",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Failed to record video",
                    Toast.LENGTH_LONG).show();
        }
    }
    

    if I remove the /NewDirectory/ the video file is saved to the root of the sd card and I get a message to that affect from my onActivityResult method.

    But with the /NewDirectory/ added I get video saved to: content:://media/external/video/media/15625

    the mediaFile.mkdirs(); is not creating the directory.

    Where have I gone wrong?

  • JTK
    JTK over 9 years
    Where should I write my files to instead?
  • weston
    weston over 9 years
    What do you mean by that last comment?
  • Blackbelt
    Blackbelt over 9 years
    @weston, officially apps have not the permission to write on the sdcard root anymore. They can still have the possibility to write in /sdcard/Android/data/app.packagename. Some vendors are not enforcing this limitation
  • Blackbelt
    Blackbelt over 9 years
    @Johntk Context has a method to access /sdcard/Android/data/app.packagename. Your app should write there. Take a look to getExternalCacheDir
  • weston
    weston over 9 years
    OK, but you will still be able to request a location using getExternalStorageDirectory and write to it yes? It's never been guaranteed to be the sd card AFAIK.
  • weston
    weston over 9 years
    Oh you mean sd-card root, I just noticed that in your second comment.
  • Blackbelt
    Blackbelt over 9 years
    @weston yep I mean on the root.
  • weston
    weston over 9 years
    So what if it makes sense to want to write to DCIM, like for pics or video that's still allowed right? It wouldn't make sense for this app presumably to write to /sdcard/Android/data/app.packagename
  • Blackbelt
    Blackbelt over 9 years
    @weston, DCIM is accessible trough the context. Tbh I don't remember exactly which method returns the path to DCIM. Some thing for Download and some other directories
  • JTK
    JTK over 9 years
    @Blackbelt is weston right, is it better for me to write to DCIM? they will be video files, but they will only be stored on the phone for a short time before they are uploaded to a mySQL DB.
  • weston
    weston over 9 years
    @Johntk if it's only temporary it makes sense to write to a cache area. The key difference is "these files will be deleted when the application is uninstalled" where as DCIM would be for pics/vids you would expect to keep after uninstall.
  • Blackbelt
    Blackbelt over 9 years
    @Johntk why would you write the video into a mySQL DB ?
  • JTK
    JTK over 9 years
    @ Blackbelt I wont It will be on a server with a pointer to it stored in the DB, i haven't got to that part yet, I'm not exactly sure how to implement it, but I'll cross that bridge when i come to it.
  • JTK
    JTK over 9 years
    @Blackbelt, I've used your getExternalCacheDir() solution and it's created the directory for me, but not saving the file to it, I've tried a few things to get it to work, can I not just concatenate + mediaFile.mp4?
  • JTK
    JTK over 9 years
    @Blackbelt, perfect, you're a gent.
  • tony gil
    tony gil about 8 years
    this worked for me. considerations regarding restrictions to write operations in certain folders depend on Android version (from kitKat 4.4 onwards they are significant).