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 available in android2.2 (API
Level8) is valid in later versions. For the equivalent calls of 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.