今天讲使用intent打开照相机、录像机的例子。
Camera
Capture a picture or video and return it
To open a camera app and receive the resulting photo or video, use the ACTION_IMAGE_CAPTURE or ACTION_VIDEO_CAPTURE action. Also specify the URI location where you'd like the camera to save the photo or video, in the EXTRA_OUTPUT extra.
Action
ACTION_IMAGE_CAPTURE or
ACTION_VIDEO_CAPTURE
Data URI Scheme
None
MIME Type
None
Extras
EXTRA_OUTPUT
The URI location where the camera app should save the photo or video file (as a Uri object).
When the camera app successfully returns focus to your activity (your app receives the onActivityResult() callback), you can access the photo or video at the URI you specified with the EXTRA_OUTPUT value.
Note: When you use ACTION_IMAGE_CAPTURE to capture a photo, the camera may also return a downscaled copy (a thumbnail) of the photo in the result Intent, saved as a Bitmap in an extra field named "data".
Example intent:
static final int REQUEST_IMAGE_CAPTURE = 1;
static final Uri mLocationForPhotos;
public void capturePhoto(String targetFilename) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.withAppendedPath(mLocationForPhotos, targetFilename));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap thumbnail = data.getParcelable("data");
// Do other work with full size photo saved in mLocationForPhotos
...
}
}
For more information about how to use this intent to capture a photo, including how to create an appropriate Uri for the output location, read Taking Photos Simply or Taking Videos Simply.
Example intent filter:
<activity ...>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
When handling this intent, your activity should check for the EXTRA_OUTPUT extra in the incoming Intent, then save the captured image or video at the location specified by that extra and call setResult() with an Intent that includes a compressed thumbnail in an extra named "data".
Start a camera app in still image mode
To open a camera app in still image mode, use the INTENT_ACTION_STILL_IMAGE_CAMERA action.
Action
INTENT_ACTION_STILL_IMAGE_CAMERA
Data URI Scheme
None
MIME Type
None
Extras
None
Example intent:
public void capturePhoto() {
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent);
}
}
Example intent filter:
<activity ...>
<intent-filter>
<action android:name="android.media.action.STILL_IMAGE_CAMERA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Start a camera app in video mode
To open a camera app in video mode, use the INTENT_ACTION_VIDEO_CAMERA action.
Action
INTENT_ACTION_VIDEO_CAMERA
Data URI Scheme
None
MIME Type
None
Extras
None
Example intent:
public void capturePhoto() {
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent);
}
}
Example intent filter:
<activity ...>
<intent-filter>
<action android:name="android.media.action.VIDEO_CAMERA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>