将视频文件存储在Android 5中的SD卡上 [英] Store video files on SD card in Android 5

查看:248
本文介绍了将视频文件存储在Android 5中的SD卡上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android.media.MediaRecorder 类录制视频,该类接受输出文件的路径字符串( MediaRecorder.setOutputFile(String) ),尽管该方法有一个版本可以接受 FileDescriptor

I'm recording video with the aid of android.media.MediaRecorder class, which accepts path string for output file (MediaRecorder.setOutputFile(String)), though there is a version of the method which accepts FileDescriptor.

我需要存储大量视频文件,所以我要使用SD卡。为了获取相关目录的路径,我使用 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)。事实证明,生成的路径用于模拟存储( / sdcard /…),而不是实际SD卡( / sdcard1 / 在我的Xperia Z3 Compact,Android 5.1.1上)。

I need to store huge video files, so I want to use SD card. In order to get path to relevant directory I use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM). It turns out that resulting path is for the "emulated" storage (/sdcard/…) instead of real SD card (/sdcard1/ on my Xperia Z3 Compact, Android 5.1.1).

我尝试对 / sdcard1 / (以及 / storage / sdcard1 ),但会出现 IOException ,该信息涉及权限拒绝。当然,我有

I tried to hardcode /sdcard1/ (as well as /storage/sdcard1) but get an IOException talking about permission deny. Of course, I have

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我听说4.4(也称为存储访问框架)之后对SD卡的访问发生了巨大变化,但是在这种情况下,如何找到解决方法的简单明了的解释不够清楚。对这个简短而简洁的解决方案有任何帮助吗?

I heard about huge changes in access to SD card after 4.4 (aka Storage Access Framework), but couldn't find simple and clear enough explanation on how to get things done in such a case. Any help on short and concise solution for this?

带有硬编码路径的PS解决方案对我来说是可以的,因为我将仅在手机上使用此应用。 / p>

PS Solution with hardcoded path would be OK for me as I'm going to use this app only with my phone.

推荐答案

此代码应该可以解决您的问题:

This code should solve your problem:

 public String createVideoFilePath() {
        String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File[] pathsArray = getExternalFilesDirs(Environment.DIRECTORY_DCIM);

/ 通常,pathArray [1]包含
可移动文件上必要文件夹的路径SD
卡。因此,我们通常只使用pathArray [1]而不是使用for语句在pathArray中搜索必需的文件
/

        for (File f : pathsArray) {
            if ((f != null) && (Environment.isExternalStorageRemovable(f))) {
                return f.getPath() + File.separator +"video_"+ time + ".mp4";
            }
        }

        return pathsArray[0].getPath() + File.separator +"video_"+ time + ".mp4";

    }


此方法返回可移动文件的位置SD卡或在模拟存储中,如果不存在可移动SD

This method returns location of file on removable SD card or in emulated storage, if removable SD doesn't exist

只需将此方法用作 MediaRecorder.setOutputFile(String)
更新:
非常重要的一点是,位于文件夹中的所有文件都是使用 Context获取的。 getExternalFilesDirs(String typr)会在卸载您的应用程序后删除。

Just use this method as a paramter of MediaRecorder.setOutputFile(String) Update: It is very important to say, that all files, which locate in folders, gotten with Context. getExternalFilesDirs(String typr) will be removed after uninstalling your app.

这篇关于将视频文件存储在Android 5中的SD卡上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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