标签:
1 package com.example.day19_03takephotos; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.provider.MediaStore; 10 import android.view.View; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 /** 21 * 拍照, 22 * 方法一:调用系统的camera app来拍照 23 * 我们先要知道camera暴漏给我们的 intent ,通过该intent才可以调器camera 拍照 24 * <intent-filter> 25 <action android:name="android.media.action.IMAGE_CAPTURE" /> 26 <category android:name="android.intent.category.DEFAULT" /> 27 </intent-filter> 28 */ 29 public void takephoto(View v){ 30 31 Intent intent = new Intent(); 32 intent.setAction("android.media.action.IMAGE_CAPTURE"); 33 //key point 应该是 你在调用camera进行拍照之前,就应该给他一个路径,告诉他照片保存在哪里 34 35 Uri uri = Uri.fromFile(new File("storage/sdcard1/picture1.jpg")); 36 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 37 startActivityForResult(intent, 100); 38 39 } 40 41 /** 42 * @param v 43 * 44 * <intent-filter> 45 <action android:name="android.media.action.VIDEO_CAPTURE" /> 46 <category android:name="android.intent.category.DEFAULT" /> 47 </intent-filter> 48 */ 49 public void takevideo(View v){ 50 Intent intent = new Intent(); 51 intent.setAction("android.media.action.VIDEO_CAPTURE"); 52 53 Uri uri = Uri.fromFile(new File("storage/sdcard1/video.mp4")); 54 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 55 startActivityForResult(intent, 200); 56 } 57 58 @Override 59 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 60 // TODO Auto-generated method stub 61 super.onActivityResult(requestCode, resultCode, data); 62 63 if (requestCode==100&&resultCode==RESULT_OK) { 64 System.out.println("MainActivity.onActivityResult()" +"拍照完成,请去处理"); 65 } 66 else if (requestCode==200&&resultCode==RESULT_OK) { 67 System.out.println("MainActivity.onActivityResult()" +"录像完成,请去处理"); 68 } 69 } 70 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.day19_03takephotos.MainActivity" 10 android:orientation="vertical"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="拍照小应用" /> 16 <Button 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:onClick="takephoto" 20 android:text="拍照" /> 21 <Button 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:onClick="takevideo" 25 android:text="录像" /> 26 27 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4544058.html