当前位置:   article > 正文

Android学习笔记——简单实现照相、录音和录像功能_keyevent 相机

keyevent 相机

        Android菜鸟第一次写原创博客,大神请轻喷,共同进步。

    最近刚接触到如何实现一个简单的照相机功能,然后又将录音和录像功能加了进去。ps:录像功能相对复杂,自己实现起来比较困难,我就直接在程序里调用系统的录像功能了。以后在慢慢学习:)好,言归正传。

1、首先新建一个照相机项目;

2、我先新建了一个BaseActivity,用于设置相机屏幕全屏和获取屏幕宽高像素等功能;

代码:

  1. package xw.phonemanager.activity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.DisplayMetrics;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. public class BaseActivity extends Activity {
  8. public static float screenWidth, screenHeigth;
  9. // private ProgressDialog mDialog;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setFullScreen(); // 设置全屏
  14. getScreen(); //获取屏幕宽高值
  15. }
  16. // 设置全屏
  17. private void setFullScreen() {
  18. requestWindowFeature(Window.FEATURE_NO_TITLE);
  19. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  20. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  21. }
  22. //获取屏幕宽高值
  23. private void getScreen() {
  24. DisplayMetrics metrics = new DisplayMetrics();
  25. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  26. screenWidth = metrics.widthPixels;
  27. screenHeigth = metrics.heightPixels;
  28. }
  29. // public void showProgressDialog(String message){
  30. // }
  31. }
3、新建一个照相的Activity(名叫PhotographActivity)继承了 BaseActivity,同时实现了OnClickListener, SurfaceHolder.Callback, PictureCallback三个接口。这时eclipse会自动提示必须实现onClick(View v)、surfaceCreated(SurfaceHolder holder)、surfaceChanged(SurfaceHolder holder, int format, int width, int height)、surfaceDestroyed(SurfaceHolder holder)、onPictureTaken(byte[] data, Camera camera)这四个方法,具体功能请看代码注释;

4、新建一个布局文件photograph.xml,用于显示打开相机后预览状态屏幕上显示的东西,和常见的相机界面差不多,如图:


xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <SurfaceView
  6. android:id="@+id/camera"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentLeft="true"
  10. android:layout_alignParentTop="true" />
  11. <SeekBar
  12. android:id="@+id/seekbar_focal"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentLeft="true"
  16. android:layout_marginLeft="20dp"
  17. android:layout_alignParentRight="true"
  18. android:layout_marginRight="60dp"
  19. android:layout_alignParentTop="true"
  20. android:layout_marginTop="15dp" />
  21. <ImageView
  22. android:id="@+id/back_btn"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignParentBottom="true"
  26. android:layout_marginBottom="10dp"
  27. android:layout_alignParentRight="true"
  28. android:layout_marginRight="10dp"
  29. android:src="@drawable/back" />
  30. <ImageView
  31. android:id="@+id/flash_switch"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_alignParentLeft="true"
  35. android:layout_marginLeft="15dp"
  36. android:layout_alignParentBottom="true"
  37. android:layout_marginBottom="10dp"
  38. android:src="@drawable/light_auto" />
  39. <ImageView
  40. android:id="@+id/switch_camera"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_alignLeft="@+id/shot_btn"
  44. android:layout_alignTop="@+id/seekbar_focal"
  45. android:src="@drawable/showall"
  46. android:visibility="invisible"/>
  47. <ImageView
  48. android:id="@+id/shot_btn"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_alignParentRight="true"
  52. android:layout_marginRight="10dp"
  53. android:layout_centerVertical="true"
  54. android:src="@drawable/photo" />
  55. <Button
  56. android:id="@+id/record_start_btn"
  57. style="?android:attr/buttonStyleSmall"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_alignParentBottom="true"
  61. android:layout_marginBottom="10dp"
  62. android:layout_toRightOf="@id/flash_switch"
  63. android:layout_marginLeft="120dp"
  64. android:text="@string/record_start" />
  65. <Button
  66. android:id="@+id/record_stop_btn"
  67. style="?android:attr/buttonStyleSmall"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_alignParentBottom="true"
  71. android:layout_marginBottom="10dp"
  72. android:layout_toRightOf="@id/flash_switch"
  73. android:layout_marginLeft="120dp"
  74. android:text="@string/record_stop"
  75. android:visibility="invisible" />
  76. <Button
  77. android:id="@+id/video_btn"
  78. style="?android:attr/buttonStyleSmall"
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:layout_alignParentBottom="true"
  82. android:layout_marginBottom="10dp"
  83. android:layout_marginLeft="73dp"
  84. android:layout_toLeftOf="@id/back_btn"
  85. android:layout_marginRight="100dp"
  86. android:text="@string/video" />
  87. </RelativeLayout>

5、开始实现相机、录音和录像功能,请看代码和注释:

  1. package xw.activity.photograph;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Calendar;
  8. import java.util.Locale;
  9. import xw.phonemanager.activity.BaseActivity;
  10. import xw.phonemanager.activity.MenuActivity;
  11. import xw.phonemanager.activity.R;
  12. import android.content.Intent;
  13. import android.content.pm.ActivityInfo;
  14. import android.graphics.Bitmap;
  15. import android.graphics.BitmapFactory;
  16. import android.graphics.PixelFormat;
  17. import android.hardware.Camera;
  18. import android.hardware.Camera.Parameters;
  19. import android.hardware.Camera.PictureCallback;
  20. import android.media.MediaRecorder;
  21. import android.net.Uri;
  22. import android.os.Bundle;
  23. import android.os.Environment;
  24. import android.provider.MediaStore;
  25. import android.text.format.DateFormat;
  26. import android.view.KeyEvent;
  27. import android.view.MotionEvent;
  28. import android.view.SurfaceHolder;
  29. import android.view.SurfaceView;
  30. import android.view.View;
  31. import android.view.View.OnClickListener;
  32. import android.view.View.OnTouchListener;
  33. import android.view.Window;
  34. import android.view.WindowManager;
  35. import android.widget.Button;
  36. import android.widget.ImageView;
  37. import android.widget.SeekBar;
  38. import android.widget.SeekBar.OnSeekBarChangeListener;
  39. import android.widget.Toast;
  40. public class PhotographActivity extends BaseActivity implements
  41. OnClickListener, SurfaceHolder.Callback, PictureCallback {
  42. SurfaceView cameraPrew;// 预览图对象
  43. ImageView shot;
  44. ImageView flash;
  45. ImageView back;
  46. ImageView switchCamera;
  47. Button recordStart;
  48. Button recordStop;
  49. Button VCR;
  50. SeekBar focus;
  51. Camera camera;// 相机对象
  52. Camera.Parameters parameters;// 相机参数对象
  53. MediaRecorder mRecorder; // MediaRecorder对象,用于实现录音和录像
  54. private int cameraNum; // 摄像头的数量
  55. private int cameraID;// 摄像头的编号
  56. private boolean canSwitchCam = false;
  57. private boolean noSD;
  58. private int maxZoom; // 最大变焦
  59. private String recFileName; // 录音音频文件名称
  60. // 设置了两个判断标识,当处于录音状态时不允许拍照或录像,均置为false
  61. private boolean isPhoto = true; // 是否可以照相
  62. private boolean isVideo = true; // 是否可以录像
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.photograph);
  67. Window window = getWindow();
  68. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 屏幕高亮
  69. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制横屏
  70. getWindow().setFormat(PixelFormat.TRANSPARENT);// 屏幕半透
  71. init();// 初始化
  72. }
  73. private void init() {
  74. checkSD();// 首先检测SD卡是否存在或可用,不可用则提示用户,避免浪费表情的尴尬:D
  75. cameraInit();
  76. prewInit();
  77. buttonInit();
  78. zoomInit();
  79. seekBarInit();
  80. }
  81. /**
  82. * 检查是否有SD卡
  83. */
  84. private void checkSD() {
  85. String SDtatus = Environment.getExternalStorageState();
  86. if (SDtatus == Environment.MEDIA_MOUNTED) {
  87. noSD = true;
  88. Toast.makeText(PhotographActivity.this, "没有SD卡或SD卡不可用",
  89. Toast.LENGTH_LONG).show();
  90. } else {
  91. noSD = false;
  92. }
  93. }
  94. /**
  95. * 相机初始化
  96. */
  97. private void cameraInit() {
  98. cameraNum = Camera.getNumberOfCameras();// 摄像头的个数
  99. if (camera != null) { // 若不为空,释放资源重新获取
  100. camera.release();
  101. }
  102. camera = Camera.open(); // 打开摄像头
  103. // 检测手机的摄像头个数,若不止一个则显示切换摄像头的图标,canSwitchCam置为true
  104. switchCamera = (ImageView) findViewById(R.id.switch_camera);
  105. if (cameraNum > 1) {
  106. switchCamera.setVisibility(View.VISIBLE);
  107. canSwitchCam = true;
  108. cameraID = 0;// 当前的摄像头ID设为0
  109. }
  110. // 相机实例获得相机参数集
  111. parameters = camera.getParameters();
  112. }
  113. /**
  114. * 预览初始化
  115. */
  116. private void prewInit() {
  117. cameraPrew = (SurfaceView) findViewById(R.id.camera);// 绑定SurfaceView并实例化
  118. cameraPrew.getHolder().setFixedSize((int) BaseActivity.screenWidth,
  119. (int) BaseActivity.screenHeigth);
  120. cameraPrew.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  121. cameraPrew.getHolder().addCallback(this);
  122. // 给整个屏幕的SurfaceView设置一个触摸监听,实现全屏手动对焦
  123. cameraPrew.setOnTouchListener(new OnTouchListener() {
  124. @Override
  125. public boolean onTouch(View v, MotionEvent event) {
  126. camera.autoFocus(null);// 点击屏幕即可对焦
  127. return false;
  128. }
  129. });
  130. }
  131. /**
  132. * 按键初始化,设置按键监听
  133. */
  134. private void buttonInit() {
  135. shot = (ImageView) findViewById(R.id.shot_btn);// 拍照键
  136. flash = (ImageView) findViewById(R.id.flash_switch);// 切换闪光灯键
  137. back = (ImageView) findViewById(R.id.back_btn);// 返回键
  138. recordStart = (Button) findViewById(R.id.record_start_btn);// 开始录音键
  139. recordStop = (Button) findViewById(R.id.record_stop_btn);// 结束录音键
  140. VCR = (Button) findViewById(R.id.video_btn);// 开始录像键
  141. // 设置监听
  142. shot.setOnClickListener(this);
  143. flash.setOnClickListener(this);
  144. back.setOnClickListener(this);
  145. switchCamera.setOnClickListener(this);
  146. recordStart.setOnClickListener(this);
  147. recordStop.setOnClickListener(this);
  148. VCR.setOnClickListener(this);
  149. }
  150. /**
  151. * 滑动条实现变焦,初始化
  152. */
  153. private void seekBarInit() {
  154. focus = (SeekBar) findViewById(R.id.seekbar_focal);
  155. focus.setProgress(0);
  156. focus.setMax(maxZoom);// 滑动条的最大值设为获得的相机可变焦值的最大值
  157. focus.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  158. @Override
  159. public void onStopTrackingTouch(SeekBar seekBar) {
  160. // TODO Auto-generated method stub
  161. }
  162. @Override
  163. public void onStartTrackingTouch(SeekBar seekBar) {
  164. // TODO Auto-generated method stub
  165. }
  166. @Override
  167. public void onProgressChanged(SeekBar seekBar, int progress,
  168. boolean fromUser) {
  169. zoomChanged(progress);// 传递当前进度,变焦
  170. }
  171. });
  172. }
  173. private void zoomInit() {
  174. maxZoom = parameters.getMaxZoom();// 获得相机最大变焦范围
  175. }
  176. /**
  177. * 按键监听
  178. */
  179. @Override
  180. public void onClick(View v) {
  181. int id = v.getId();
  182. switch (id) {
  183. case R.id.shot_btn:
  184. if (isPhoto == true) {
  185. takPicture();
  186. } else {
  187. Toast.makeText(this, "无响应,正在录音", Toast.LENGTH_SHORT).show();
  188. }
  189. break;
  190. case R.id.flash_switch:
  191. flashSwitch();
  192. break;
  193. case R.id.back_btn:
  194. startActivity(new Intent(PhotographActivity.this,
  195. MenuActivity.class));
  196. finish();
  197. break;
  198. // 切换摄像头
  199. case R.id.switch_camera:
  200. if (canSwitchCam == true) {// 至少有两个才能切换
  201. if (camera != null) {
  202. camera.stopPreview();
  203. camera.release();
  204. Camera.open(++cameraID % cameraNum);// 在id:0~cameraNum之间切换
  205. }
  206. }
  207. break;
  208. case R.id.record_start_btn:
  209. if (noSD == true) { // 没有SD卡
  210. Toast.makeText(PhotographActivity.this, "没有SD卡或SD卡不可用",
  211. Toast.LENGTH_LONG).show();
  212. return;
  213. } else {
  214. startRec();// 开始录音
  215. }
  216. break;
  217. case R.id.record_stop_btn:
  218. stopRec();// 结束录音
  219. break;
  220. case R.id.video_btn:
  221. if (isVideo == true) {
  222. startVideo();// 开始录像
  223. } else {
  224. Toast.makeText(this, "无响应,正在录音", Toast.LENGTH_SHORT).show();
  225. }
  226. break;
  227. }
  228. }
  229. /**
  230. * 切换闪光灯状态,如果当前是开状态,点击切换为关状态, 再点击切换为自动状态,再点击又到开状态
  231. */
  232. private void flashSwitch() {
  233. if (parameters.getFlashMode() == Parameters.FLASH_MODE_AUTO) {
  234. flash.setImageResource(R.drawable.light_on);
  235. parameters.setFlashMode(Parameters.FLASH_MODE_ON);
  236. camera.setParameters(parameters);
  237. } else if (parameters.getFlashMode() == Parameters.FLASH_MODE_ON) {
  238. flash.setImageResource(R.drawable.light_off);
  239. parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
  240. camera.setParameters(parameters);
  241. } else {
  242. flash.setImageResource(R.drawable.light_auto);
  243. parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
  244. camera.setParameters(parameters);
  245. }
  246. }
  247. // 变焦
  248. private void zoomChanged(int zoom) {
  249. parameters.setZoom(zoom);
  250. camera.setParameters(parameters);
  251. }
  252. // 照像
  253. public void takPicture() {
  254. camera.autoFocus(null);
  255. camera.takePicture(null, null, this);
  256. }
  257. @Override
  258. public void surfaceCreated(SurfaceHolder holder) {
  259. try {
  260. camera.setPreviewDisplay(cameraPrew.getHolder());// 获取相机预览
  261. } catch (IOException e) {
  262. e.printStackTrace();
  263. }
  264. camera.getParameters();// 获取相机参数实例
  265. camera.startPreview();
  266. }
  267. @Override
  268. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  269. int height) {
  270. // TODO Auto-generated method stub
  271. }
  272. /**
  273. * 退出相机界面后,释放相机相关资源
  274. */
  275. @Override
  276. public void surfaceDestroyed(SurfaceHolder holder) {
  277. if (camera != null) {
  278. camera.stopPreview();
  279. camera.release();
  280. camera = null;
  281. }
  282. }
  283. /**
  284. * 拍照并保存,自动跳转到查看界面
  285. */
  286. @Override
  287. public void onPictureTaken(byte[] data, Camera camera) {
  288. if (noSD == true) { // 没有SD卡
  289. Toast.makeText(PhotographActivity.this, "没有SD卡或SD卡不可用",
  290. Toast.LENGTH_LONG).show();
  291. return;
  292. } else {
  293. Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  294. String name = DateFormat.format("yyyyMMdd_hhmmss",
  295. Calendar.getInstance(Locale.CHINA))
  296. + ".jpg";// 自定义照片名字和格式
  297. File file = new File("sdcard/TianTian/photo/");
  298. file.mkdirs(); // 创建目录
  299. String fileName = "/mnt/sdcard/TianTian/photo/" + name;// 照片文件的绝对路径
  300. BufferedOutputStream bos = null;
  301. try {
  302. bos = new BufferedOutputStream(new FileOutputStream(fileName));
  303. bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);// 通过缓冲流存入
  304. // 拍完之后跳转到查看刚拍的照片的一个Activity(ConfirmActivity),用户可以选择是否保留图片
  305. ConfirmActivity.photoName = fileName;
  306. Uri imgUri = Uri.fromFile(new File(fileName));
  307. Intent intent = new Intent();
  308. intent.setClass(PhotographActivity.this, ConfirmActivity.class);
  309. intent.setData(imgUri);
  310. this.camera.stopPreview();// 停止预览
  311. startActivity(intent);
  312. } catch (FileNotFoundException e) {
  313. e.printStackTrace();
  314. } finally {
  315. try {
  316. bos.close();// 关闭流
  317. } catch (IOException e) {
  318. e.printStackTrace();
  319. }
  320. }
  321. }
  322. }
  323. // 开始录音
  324. private void startRec() {
  325. // 录音时不能拍照和录像
  326. isPhoto = false;
  327. isVideo = false;
  328. mRecorder = new MediaRecorder();
  329. mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置录音员,从MIC获得声音
  330. mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置音频输出格式
  331. mRecorder.setAudioEncoder(MediaRecorder.VideoEncoder.DEFAULT);// 设置编码格式
  332. String path = Environment.getExternalStorageDirectory().getPath();
  333. String name = DateFormat.format("yyyyMMdd_hhmmss",
  334. Calendar.getInstance(Locale.CHINA))
  335. + ".3gp";
  336. File file = new File(path);
  337. file.mkdirs();
  338. recFileName = path + "/" + name;
  339. file = new File(recFileName);
  340. if (!file.exists()) {
  341. try {
  342. file.createNewFile();
  343. } catch (IOException e) {
  344. e.printStackTrace();
  345. }
  346. }
  347. mRecorder.setOutputFile(recFileName);
  348. try {
  349. mRecorder.prepare();
  350. } catch (IllegalStateException e) {
  351. e.printStackTrace();
  352. } catch (IOException e) {
  353. e.printStackTrace();
  354. }
  355. mRecorder.start(); // 录音开始
  356. Toast.makeText(this, "录音开始", Toast.LENGTH_SHORT).show();
  357. // “开始录音”后,将按钮换成“停止录音”获得焦点
  358. recordStart.setVisibility(View.GONE);
  359. recordStop.setVisibility(View.VISIBLE);
  360. }
  361. // 停止录音
  362. private void stopRec() {
  363. isPhoto = true;
  364. isVideo = true;
  365. mRecorder.stop();
  366. mRecorder.release();
  367. mRecorder = null;
  368. Toast.makeText(this, "录音结束", Toast.LENGTH_SHORT).show();
  369. // “停止录音”后,将按钮还原“开始录音”获得焦点
  370. recordStop.setVisibility(View.GONE);
  371. recordStart.setVisibility(View.VISIBLE);
  372. }
  373. /**
  374. * 调用系统录像机录像
  375. */
  376. private void startVideo() {
  377. camera.stopPreview();
  378. camera.release();
  379. camera = null;
  380. Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  381. intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
  382. startActivityForResult(intent, RESULT_FIRST_USER);
  383. finish();
  384. }
  385. /**
  386. * 重写onKeyDown方法,当在相机界面按返回键退到主菜单而不是直接退出程序
  387. */
  388. @Override
  389. public boolean onKeyDown(int keyCode, KeyEvent event) {
  390. if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
  391. startActivity(new Intent(PhotographActivity.this,
  392. MenuActivity.class));
  393. finish();
  394. }
  395. return false;
  396. }
  397. }

6、增加一个拍完照后可以查看所拍的照片的功能,并且可以选择保留或者删除当前的照片,选择后均会回到相机界面。新建一个ConfirmActivity,继承BaseActivity。代码:

  1. package xw.activity.photograph;
  2. import java.io.File;
  3. import xw.phonemanager.activity.BaseActivity;
  4. import xw.phonemanager.activity.R;
  5. import android.content.Intent;
  6. import android.content.pm.ActivityInfo;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.view.KeyEvent;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.ImageView;
  13. import android.widget.Toast;
  14. public class ConfirmActivity extends BaseActivity {
  15. public static String photoName;
  16. ImageView photo;// 显示所拍的照
  17. ImageView save;// 保留
  18. ImageView delete;// 删除
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.confirm);
  23. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 强制横屏
  24. photo = (ImageView) findViewById(R.id.photo_img);
  25. save = (ImageView) findViewById(R.id.photo_save);
  26. // 设置监听,点击后会自动回到相机界面
  27. save.setOnClickListener(new OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. Toast.makeText(ConfirmActivity.this, "照片已保存", Toast.LENGTH_LONG)
  31. .show();
  32. startActivity(new Intent(ConfirmActivity.this,
  33. PhotographActivity.class));
  34. finish();
  35. }
  36. });
  37. delete = (ImageView) findViewById(R.id.photo_delete);
  38. // 设置监听,点击后会自动回到相机界面
  39. delete.setOnClickListener(new OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. File file = new File(photoName);
  43. if (file.exists()) {
  44. file.delete();// 删除照片
  45. Toast.makeText(ConfirmActivity.this, "照片已删除",
  46. Toast.LENGTH_LONG).show();
  47. startActivity(new Intent(ConfirmActivity.this,
  48. PhotographActivity.class));
  49. finish();
  50. }
  51. }
  52. });
  53. showPic();// 显示所拍的照片
  54. }
  55. private void showPic() {
  56. Intent intent = getIntent();
  57. Uri originUri = intent.getData();
  58. photo.setImageURI(originUri);
  59. }
  60. /**
  61. * 重写onKeyDown方法,当在相机界面按返回键退到主菜单而不是直接退出程序
  62. */
  63. @Override
  64. public boolean onKeyDown(int keyCode, KeyEvent event) {
  65. if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
  66. startActivity(new Intent(ConfirmActivity.this,
  67. PhotographActivity.class));
  68. finish();
  69. }
  70. return false;
  71. }
  72. }

与之对应的xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <ImageView
  6. android:id="@+id/photo_img"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"/>
  9. <ImageView
  10. android:id="@+id/photo_delete"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentRight="true"
  14. android:layout_alignTop="@+id/photo_save"
  15. android:layout_marginRight="74dp"
  16. android:src="@drawable/photo_del" />
  17. <ImageView
  18. android:id="@+id/photo_save"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentBottom="true"
  22. android:layout_alignParentLeft="true"
  23. android:layout_marginBottom="32dp"
  24. android:layout_marginLeft="68dp"
  25. android:src="@drawable/photo_save" />
  26. </RelativeLayout>

至此,简单的照相、录音和录像功能基本实现,照相界面效果图:




学识鄙陋,未免贻笑大方,望见谅:)

谢谢。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/713828
推荐阅读
相关标签
  

闽ICP备14008679号