Android mkdirs () not working

I am developing my first Android application, and I had a problem trying to create a directory for saving recorded video files.

I have a method in my main action buttonOnClickRecordthat causes the intention to use an Android camera, I also create a file during this method call, and I call a method mkdirs()on it to create a directory to store the file.

I 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 delete /NewDirectory/, the video file will be saved in the root folder of the SD card, and I get a message from this method from my method onActivityResult.

But when adding, /NewDirectory/I get the video saved in:content:://media/external/video/media/15625

mediaFile.mkdirs(); does not create a directory.

Where am I wrong?

+4
2

myvideo.mp4.

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

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

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

getExternalCacheDir()

, kitkat sdcard .

: :

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

:

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

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

:

File sdcard = Environment.getExternalStorageDirectory();
File directory = new File(sdcard.getAbsolutePath() + "/NewDirectory");
File video = new File(directory, fileName);
+2

All Articles