How to save video file in SD card in android

3.8k views Asked by At

I am trying to save Video file on SD card but getting null pointer. Have a look of my code.

I just need to create a folder in SD Card and save videos on it. When i have not using fileUri then not got Crash.

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

Thanks. Suggestion appreciated.

2

There are 2 answers

0
bhumika rijiya On

Try this,

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

Refer this link,

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

I wish it will help you.

0
Siddhartha Maji On

To Save File on SD Card Follow the code.

add "android.permission.WRITE_EXTERNAL_STORAGE" in manifest file."

And then use the code -

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