Android的mkdirs()不工作 [英] Android mkdirs() not working

查看:474
本文介绍了Android的mkdirs()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I''m发展我的第一个Android应用程序,我心中已经遇到了问题,而试图创建一个目录来保存录制的视频文件。

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.

我在我的主要活动的方法 buttonOnClickRecord 这个方法调用期间调用的意图使用Android相机,我也创建一个文件,我调用 mkdirs()方法上它来创建存储文件的目录。

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.

我也实施<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/> 在我的清单

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();
    }
}

如果我删除 / NewDirectory / 视频文件保存到SD卡的根,我得到一个消息,从我的的onActivityResult 方法。

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.

但与 / NewDirectory / 加入我得到的视频保存到:内容:: //媒体/外部/视频/媒体/ 15625

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

mediaFile.mkdirs(); 不是创建目录

我在哪里出了错?

推荐答案

您正试图创建一个名为 myvideo.mp4

You are trying to create a directory called 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()

是在位于SD卡的根目录写入奇巧意识到不再被允许的。

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

编辑:文件路径应该是:

the path to the file should be:

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

这篇关于Android的mkdirs()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆