A simple example of the android camera function

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.