Development Example – Camera Applet (Video)

This example shows how launch the camera from an application in order to record a video and how to specify the desired location (if any) at which to save the video file.

Launching the camera

In the simplest instance, you can use the standard android media intent in order to make a request to capture video.

private final static int REQUEST_ID = 123; 

... 

public void onLaunchCameraBasic(View view) { 
     final Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
     startActivityForResult(videoIntent, REQUEST_ID); 
}

 

Launching the camera and specifying a custom location for the video file

In this case, a FileProvider must be used in order to create a content URI with temporary read and write permissions.

The FileProvider must be specified in the AndroidManifest.xml file, with the following attributes:

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
          android:name="android.support.FILE_PROVIDER_PATHS" 
          android:resource="@xml/file_paths" /> 
</provider>

As the FileProvider requires directories which will be used to be specified beforehand, one or more child elements of the <paths> element should be added to the res/xml/file_paths.xml file.

<paths> 
     <external-path name="my_images" path="Android/data/com.realwear.hmt1developerexamples/files/Movies" /> 
</paths>

This path component must correspond to the path used to create the output file in the java code. In this case the File that corresponds is the following:

final File mediaStorageDir = getExternalFilesDir(Environment.DIRECTORY_MOVIES);

Finally, to invoke the camera intent, the URI is given by the FileProvider and passed into the intent.

public void onLaunchCameraFileProvider(View view) { 
     try { 
          final Uri videoUri = FileProvider.getUriForFile( 
                    getApplicationContext(), 
                    getApplicationContext().getPackageName() + ".fileprovider", 
                    generateFile()); 

          final Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
          videoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
          videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri); 
          startActivityForResult(videoIntent, REQUEST_ID); 
     } catch (IOException e) { 
          Log.e(TAG, "Error creating video file.", e); 
     }
}