Capture a video and store it at a specific location rather than a default location

后端 未结 3 1925
长发绾君心
长发绾君心 2021-01-03 14:07

I want to capture video and store video at specific location other than default location.

I know there is a method with MediaStore called setOutPutFile(\"Strin

相关标签:
3条回答
  • 2021-01-03 14:15

    The answer given by abhi here is correct, but i am writing a new answer to make it more easy and simple to other users.

    To record a video, write this code in your onCreate method or on button click where you want to start the video recording

    private static final int ACTION_TAKE_VIDEO = 3;
    
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
    

    This code will start video recording.

    Now below in your code after the onCreate method you need to override a method which name is onActivityResult.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (resultCode == RESULT_OK) 
        {
            try
            {
                AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                FileInputStream fis = videoAsset.createInputStream();
                File root=new File(Environment.getExternalStorageDirectory(),"/RecordVideo/");  //you can replace RecordVideo by the specific folder where you want to save the video
                if (!root.exists()) {
                    System.out.println("No directory");
                    root.mkdirs();
                }
    
                File file;
                file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
    
                FileOutputStream fos = new FileOutputStream(file);
    
                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }       
                fis.close();
                fos.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    }
    

    Replace "RecordVideo" with the specific folder where you want to the video file on your sd card. You can also use any existing folder or directory name here.

    Thanks

    0 讨论(0)
  • 2021-01-03 14:19

    I wasn't happy with any of the solutions provided here. Here's an extremely simple solution that will save the video in folder of your choice.

    private static final int REQUEST_VIDEO_CAPTURE = 300;
    private Uri videoUri;
    
    private void dispatchVideoIntent(){
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        File outputFile = new File("your file path goes here");
        videoUri = Uri.fromFile(outputFile);
        takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK && videoUri != null) {
                // do what you want with videoUri
        }
    }
    
    0 讨论(0)
  • 2021-01-03 14:21

    Do in this way :

    Globally declare it

    public static final int TAKE_PICTURE=0;
    

    and then

    Intent photoPickerIntent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                          startActivityForResult(Intent.createChooser(photoPickerIntent,"Take Video"),TAKE_VIDEO);
    

    In OnActivityResult Handle in this way:

    public void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            if (resultCode == RESULT_OK) 
             {
                    if(requestCode==TAKE_VIDEO)
                {
    
    
                    try
                    {
                        Log.e("videopath","videopath");
                    AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                    FileInputStream fis = videoAsset.createInputStream();
                    File root=new File(Environment.getExternalStorageDirectory(),"Directory Name");
    
                      if (!root.exists()) {
                          root.mkdirs();
                      }
    
                      File file;
                      file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
    
                    FileOutputStream fos = new FileOutputStream(file);
    
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = fis.read(buf)) > 0) {
                        fos.write(buf, 0, len);
                    }       
                    fis.close();
                    fos.close();
                  } 
                catch (Exception e) 
                {
                   e.printStackTrace();
                }
                 }
            }
    

    Use below permissions:

    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    
    0 讨论(0)
提交回复
热议问题