Android Camera series development (1): Taking photos with Intent

Source: Internet
Author: User

Overview

There are two ways to use Camera: using an existing app through Intent and building your own app through Camera.

Camera statements

If your application wants to use Camera, you must obtain the license and add the following statement to AndroidManifest. xml.

<Uses-permissionandroid: name = "android. permission. CAMERA"/>

If your application must have a Camera to use, the statement is as follows:

<Uses-featureandroid: name = "android. hardware. camera"/>

Otherwise, the statement is as follows:

<Uses-featureandroid: name = "android. hardware. camera" android: required = "false"/>

Other statements:

Storage: <uses-permissionandroid: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

Voice: <uses-permissionandroid: name = "android. permission. RECORD_AUDIO"/>

Location: <uses-permissionandroid: name = "android. permission. ACCESS_FINE_LOCATION"/>

 

Core

Itent: it is easier to program Camera.

Camera: it allows you to control cameras and build more powerful Camera applications.

SurfaceView: Provides the Real-Time preview function.

 

Using Itent for photo taking

Step 1: Create an Android project named Photo in Eclipse. For details, refer to the Hello world example;

Step 2: Add Camera-related declarations in AndroidManifest. xml as follows:

<Uses-permissionandroid: name = "android. permission. CAMERA"/>

Step 3: Compile the MainActivity class.

(1) Add member variables

(2) modify the OnCreate Method

(3) added the onActivityResult method.

(4) two auxiliary methods are added.

The source code of the class is as follows:

[Java]
Public class MainActivity extends Activity {
Private static final int MEDIA_TYPE_IMAGE = 1;
Private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

Private Intent intent = null;
Private Uri fileUri = null;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // create a intent to take picture
FileUri = getOutputMediaFileUri (MEDIA_TYPE_IMAGE); // create a file to save the image
Intent. putExtra (MediaStore. EXTRA_OUTPUT, fileUri); // set the image file name
 
// Start the image capture Intent
StartActivityForResult (intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
 
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );

If (requestCode = CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ){
If (resultCode = RESULT_ OK ){
// Image captured and saved to fileUri specified in the Intent
Toast. makeText (this, "Image saved to: \ n" +
Data. getData (),
Toast. LENGTH_LONG). show ();
} Else if (resultCode = RESULT_CANCELED ){
// User canceled the image capture
} Else {
// Image capture failed, advise user
}
}
}

/** 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.
 
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 = null;
If (type = MEDIA_TYPE_IMAGE ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"IMG _" + timeStamp + ". jpg ");
}
 
Return mediaFile;
}
}

Public class MainActivity extends Activity {
Private static final int MEDIA_TYPE_IMAGE = 1;
Private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
 
Private Intent intent = null;
Private Uri fileUri = null;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // create a intent to take picture
FileUri = getOutputMediaFileUri (MEDIA_TYPE_IMAGE); // create a file to save the image
Intent. putExtra (MediaStore. EXTRA_OUTPUT, fileUri); // set the image file name

// Start the image capture Intent
StartActivityForResult (intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}

@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );

If (requestCode = CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ){
If (resultCode = RESULT_ OK ){
// Image captured and saved to fileUri specified in the Intent
Toast. makeText (this, "Image saved to: \ n" +
Data. getData (),
Toast. LENGTH_LONG). show ();
} Else if (resultCode = RESULT_CANCELED ){
// User canceled the image capture
} Else {
// Image capture failed, advise user
}
}
}
 
/** 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.

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 = null;
If (type = MEDIA_TYPE_IMAGE ){
MediaFile = new File (mediaStorageDir. getPath () + File. separator +
"IMG _" + timeStamp + ". jpg ");
}

Return mediaFile;
}
}

 
Step 4: run the program.

If the program crashes when the response is taken, the following error occurs when you view the log:

Java. lang. RuntimeException: Failure delivering resultResultInfo {who = null, request = 100, result =-1, data = null} to activity

This problem can be solved by adding the android: launchMode = "singleInstance" attribute to the activity element in AndroidManifest. xml.

After taking the photo, you can find the saved photo in the Pictures/MyCameraApp directory of the SD card.

 

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.