Save a media file
Media files such as images and videos created by users should be stored in the external storage directory (SD card) of the device to save system space, and allow users to access these files outside of the device. There are many directory locations on the device that are willing to save media files, but as a developer, there should be only two standard locations:
Evironment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES): This method returns a standard, shared, and recommended directory location. This directory is shared (public), so other applications can easily find, read, change, and delete saved files at this location. If your application is uninstalled, the media files stored in this location will not be deleted. To avoid interference with existing images and videos, you should create a sub-directory for your application in this directory as shown in the code below. This method is valid in Versions later than Android2.2 (API Level8). For equivalent calls to earlier API versions, see "Save shared files"
Context. getExternalFilesDir (Environment. DIRECTORY_PICTURES): This method returns a location associated with your application for saving images and videos. If your application is uninstalled, any files stored in this location will be deleted. The security of files in this location is not mandatory. Other applications can read, change, and delete them.
The following sample code demonstrates how to create a File or Uri object for a media File, which can be used to call the Intent object of the device Camera, or as part of building a Camera application.
Public static final int MEDIA_TYPE_IMAGE = 1;
Public static final int MEDIA_TYPE_VIDEO = 2;
/** Create a file Uri for saving an image or video */
Private static Uri getOutputMediaFileUri (int type ){
Return Uri. fromFile (getOutputMediaFile (type ));
}
/** Create a File for saving an image or video */
Private static File getOutputMediaFile (int type ){
// To be safe, you should check that the SDCard is mounted
// Using Environment. getExternalStorageState () before doing this.
File mediaStorageDir = new File (Environment. getExternalStoragePublicDirectory (
Environment. DIRECTORY_PICTURES), "MyCameraApp ");
// This location works best if you want the created images to be shared
// Between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
If (! MediaStorageDir. exists ()){
If (! MediaStorageDir. mkdirs ()){
Log. d ("MyCameraApp", "failed to create directory ");
Return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss"). format (new Date ());
File mediaFile;
If (type = MEDIA_TYPE_IMAGE ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"IMG _" + timeStamp + ". jpg ");
} Else if (type = MEDIA_TYPE_VIDEO ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"VID _" + timeStamp + ". mp4 ");
} Else {
Return null;
}
Return mediaFile;
}
Note: Environment. getExternalStoragePublicDirectory () is valid after Android2.2 (API Level 8). If your target device is of an earlier Android version, use Environment. getExternalStorageDirectory.
Camera functions:
Android provides a broad array of Camera functions that the Camera application can control, such as the chip format, flash mode, and focus settings. This section lists common Camera features and briefly discusses how to use them. Most Camera functions can be accessed and set through the Camera. Parameters object. However, there are several important features that require simpler settings than Camera. Parameters. These features are included in the following sections:
1. metering and focusing
2. Facial Recognition
3. Delayed photography
For more information about how to use these control functions through Camera. Parameters, see "using the Camera function ". For more details about how to use these control functions through the Camera parameter object, the function items in the following table are linked to the specified API documentation.
Table 1. These Camera functions are sorted by the version of the Android API Level they are introduced.
Function
API Level
Introduction
Face Detection
14
Mark the face inside the image, and use this logo for focusing, measuring, and white balance processing.
Metering Areas
14
Specify multiple points in the image for White Balance Calculation
Focus Areas
14
Set multiple points in the image for focusing
White Balance Lock
14
Automatically stop or start White Balance Adjustment
Exposure Lock
14
Automatic Stop or start exposure Adjustment
Video Snapshot
14
Get a photo in the video (frame acquisition)
Time Lapse Video
11
Record delayed videos with delayed Frames
Multiple Cameras
9
Multiple Camera are supported on one device, including the front and back Camera
Focus Distance
9
Report the distance between Camera and the focus object
Zoom
8
Set the image scaling rate
Exposure Compensation
8
Increase or decrease the exposure level
GPS Data
5
Include or ignore geographic location data in an image
White Balance
5
Sets the white block mode, which affects the color values in the collected image.
Focus Mode
5
Set the focus mode of Camera, such as automatic, fixed, Macro, or infinite
Scene Mode
5
Preset a specific photography environment, such as night, beach, snow, and dusk
JPEG Quality
5
Sets the compression level of a JPEG image, which increases or reduces the file quality and size of the image output.
Flas Mode
5
Switch the flashlight or use automatic settings
Color Effects
5
Set the color effect of the collected image, such as the black and white effect, the brown tone or the reversed color.
Anti-Banding
5
Reduce the effect of color gradient caused by JPEG compression
Picture Format
1
File Format of the specified Image
Picture Size
1
Specify the pixel size of the saved Image
Note: Due to different hardware and software implementations, these features are not supported by all devices.