本文出自 “whithin's” 博客,请务必保留此出处http://whithin.blog.51cto.com/690417/1058972
Sample code,实现简单应用界面包含两个button,点击button 分别打开camera, 和cameroder,主界面如下图所示
1. 在AndroidManifest.xml文件中包含camera 相关的权限
AndroidManifest.xml
<?xml version= "1.0" encoding ="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.study.sample"
android:versionCode= "1"
android:versionName= "1.0" >
<uses-sdk android:minSdkVersion= "15" />
<uses-permission android:name= "android.permission.CAMERA" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="android.study.sample.CameraIntentActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application >
</manifest>
2.在主界面的layout文件中添加两个button,分别负责打开拍照和录视频的应用
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
res/layout/main.xml
<? xml version= "1.0" encoding = "utf-8"?>
< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<Button
android:id ="@+id/buttonTakePic"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_alignParentTop ="true"
android:layout_centerHorizontal ="true"
android:layout_marginTop ="100dp"
android:text ="takepicture" />
<Button
android:id ="@+id/buttonRecordVideo"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_alignParentLeft ="true"
android:layout_alignParentTop ="true"
android:layout_marginTop ="42dp"
android:text ="recordvideo" />
</ RelativeLayout>
3.通过发送不同的Intent打开拍照和录视频的应用
MediaStore.ACTION_IMAGE_CAPTURE MediaStore.ACTION_VIDEO_CAPTURE 程序调用startActivityForResult(Intent, int, Bundle)
接口启动相应的应用程序, 并且必须Activity实现onActivityResult()
来接受调用相应的Camera应用activity之后的结果
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
android.study.sample/CameraIntentActivity.java
package android.study.sample;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
publicclass CameraIntentActivity extends Activity{
publicstaticfinalint MEDIA_TYPE_IMAGE = 1;
publicstaticfinalint MEDIA_TYPE_VIDEO = 2;
privatestaticfinalint CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
privatestaticfinalint CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri ;
Button takePicureButton;
Button recordVideoButton;
OnClickListener cameraButtonListenser = new OnClickListener(){
publicvoid onClick(View view) {
// TODO Auto-generated method stub
Intent intent = null;
if(((Button)view).equals(takePicureButton )){
// create Intent to take a picture and return control to the calling application
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the p_w_picpath
intent.putExtra(MediaStore. EXTRA_OUTPUT, fileUri ); // set the p_w_picpath file name
// start the video record Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
} elseif (((Button)view).equals(recordVideoButton)){
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore. EXTRA_OUTPUT, fileUri ); // set the video file name
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE );
}
}
};
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. main);
takePicureButton = (Button)findViewById(R.id.buttonTakePic);
recordVideoButton = (Button)findViewById(R.id.buttonRecordVideo);
takePicureButton.setOnClickListener(cameraButtonListenser );
recordVideoButton.setOnClickListener(cameraButtonListenser );
}
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
if(data!=null ){
Toast. makeText(this, "Image saved to:\n" +
data.getData().toString(), Toast.LENGTH_LONG).show();
} else{
System. out.println("can't get any data!!!" );
}
} elseif (resultCode == RESULT_CANCELED) {
// User cancelled the p_w_picpath capture
} else {
// Image capture failed, advise user
}
}
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
if(data!=null ){
Toast. makeText(this, "Image saved to:\n" +
data.getData().toString(), Toast.LENGTH_LONG).show();
} else{
System. out.println("can't get any data!!!" );
}
} elseif (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
}
/** Create a file Uri for saving an p_w_picpath or video */
privatestatic Uri getOutputMediaFileUri(int type){
return Uri.fromFile( getOutputMediaFile(type));
}
/** Create a File for saving an p_w_picpath or video */
privatestatic 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 p_w_picpaths 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");
returnnull ;
}
}
// 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" );
} elseif (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4" );
} else {
returnnull ;
}
return mediaFile;
}
}
本文出自 “whithin's” 博客,请务必保留此出处http://whithin.blog.51cto.com/690417/1058972
转载于:https://blog.51cto.com/yuzuolin/1330249