In android, the camera function system has been provided and can be directly used in apps. When a mobile phone downloads a photo app from android play, it determines whether the mobile phone supports the app. No. Download is not supported.
There are several steps for photography:
1. Declare Permissions
2. Use Camera for photography
3. display images
1. Declare Permissions
Declare the use of Camera in manifest:
Copy codeThe Code is as follows: <uses-feature android: name = "android. hardware. camera"/>
2. Use Camera for photography
In the Activity, call the Camera Application
Copy codeThe Code is as follows: private void dispatchTakePictureIntent (int actionCode ){
Intent takePictureIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (takePictureIntent, actionCode );
}
3. display images
After you use Camera to take a photo, it will return. to display the image, you must first obtain the image and then display it.
Obtain from the onActivityResult Method
Copy codeThe code is as follows: <PRE class = java name = "code"> @ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (requestCode ){
Bundle extras = intent. getExtras ();
Bitmap mImageBitmap = (Bitmap) extras. get ("data ");
MImageView. setImageBitmap (mImageBitmap );
} </PRE>
<PRE> </PRE>
<PRE> </PRE>
To save the image to the specified directory, you must specify the fileCopy codeThe Code is as follows: Intent takePictureIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
File f = null;
Try {
F = setUpPhotoFile ();
TakePictureIntent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (f ));
} Catch (IOException e ){
E. printStackTrace ();
F = null;
}
Copy codeThe Code is as follows: private File createImageFile () throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss"). format (new Date ());
String imageFileName = "IMG _" + timeStamp + "_";
File albumF = getAlbumDir ();
File imageF = File. createTempFile (imageFileName, "jpg", albumF );
Return imageF;
}
Private File setUpPhotoFile () throws IOException {
File f = createImageFile ();
MCurrentPhotoPath = f. getAbsolutePath ();
Return f;
}
Private File getAlbumDir (){
File storageDir = null;
If (Environment. MEDIA_MOUNTED.equals (Environment. getExternalStorageState ())){
StorageDir = mAlbumStorageDirFactory. getAlbumStorageDir (getAlbumName ());
If (storageDir! = Null ){
If (! StorageDir. mkdirs ()){
If (! StorageDir. exists ()){
Log. d ("CameraSample", "failed to create directory ");
Return null;
}
}
}
} Else {
Log. v (getString (R. string. app_name), "External storage is not mounted READ/WRITE .");
}
Return storageDir;
}