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

后端 未结 3 1943
长发绾君心
长发绾君心 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: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:

    
    
    

提交回复
热议问题