如何在android中保存SD卡中的视频文件

时间:2014-11-18 07:23:47

标签: android video android-sdcard video-recording

我正在尝试将视频文件保存在SD卡上但获取空指针。 看看我的代码。

我只需要在SD卡中创建一个文件夹并在其上保存视频。 当我没有使用fileUri时,没有得到崩溃。

File mediaFile = new
                File(Environment.getExternalStorageDirectory().getAbsolutePath() 
                        + "/myvideo"+System.currentTimeMillis() +".mp4");   
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        fileUri = Uri.fromFile(mediaFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, VIDEO_CAPTURE);
     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == VIDEO_CAPTURE) {
            if (resultCode == Activity.RESULT_OK) {
                 Toast.makeText(getActivity(), "Video has been saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(getActivity(), "Video recording cancelled.", 
                      Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getActivity(), "Failed to record video", 
                      Toast.LENGTH_LONG).show();
            }
        }
                    }

感谢。 建议表示赞赏。

2 个答案:

答案 0 :(得分:1)

在SD卡上保存文件请遵循以下代码。

在清单文件中添加" android.permission.WRITE_EXTERNAL_STORAGE" 。"

然后使用代码 -

filePath = getExternalFilesDirs("/")[1].toString();
 OutputStreamWriter writer;
        File file;
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

file = new File(filePath + "yourfilename.txt");
writer = new OutputStreamWriter(ostream, "UTF-8");

//save your video file here

writer.close();

答案 1 :(得分:0)

试试这个,

 private static File getOutputMediaFile(int type){

        // Check that the SDCard is mounted
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraVideo");


        // Create the storage directory(MyCameraVideo) if it does not exist
        if (! mediaStorageDir.exists()){

            if (! mediaStorageDir.mkdirs()){

                output.setText("Failed to create directory MyCameraVideo.");

                Toast.makeText(ActivityContext, "Failed to create directory MyCameraVideo.", 
                        Toast.LENGTH_LONG).show();

                Log.d("MyCameraVideo", "Failed to create directory MyCameraVideo.");
                return null;
            }
        }


        // Create a media file name

        // For unique file name appending current timeStamp with file name
        java.util.Date date= new java.util.Date();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                             .format(date.getTime());

        File mediaFile;

        if(type == MEDIA_TYPE_VIDEO) {

            // For unique video file name appending current timeStamp with file name
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "VID_"+ timeStamp + ".mp4");

        } else {
            return null;
        }

        return mediaFile;
    }

参考此链接,

http://androidexample.com/Camera_Video_Capture_And_Save_On_SDCard_-_Android_Example/index.php?view=article_discription&aid=123

我希望它会对你有所帮助。