Android: Video Recording Throws Error

前端 未结 1 1019
北海茫月
北海茫月 2021-01-07 07:10

So I\'m trying to use the built in camera activity to record a video using the below code:

    Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTUR         


        
相关标签:
1条回答
  • 2021-01-07 07:57

    Actually, I found in some case MediaStore.EXTRA_OUTPUT doesn't work properly, SO the other trick way is, store your captured video file in onActivityResult()

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
    
      if (resultCode == RESULT_OK) 
       {   
        try {
             AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
             FileInputStream fis = videoAsset.createInputStream();
             File videoFile = new File(Environment.getExternalStorageDirectory(),"<VideoFileName>.mp4"); 
             FileOutputStream fos = new FileOutputStream(videoFile);
    
             byte[] buffer = new byte[1024];
             int length;
             while ((length = fis.read(buffer)) > 0) {
                   fos.write(buffer, 0, length);
              }       
             fis.close();
             fos.close();
           } catch (IOException e) {
              // TODO: handle error
             }
        }
     }
    

    Try the above code and let me know about your success.

    0 讨论(0)
提交回复
热议问题