Android Studio调用摄像头

分手后的思念是犯贱 2022-04-15 03:16 714阅读 0赞

演示代码的功能,调用摄像头拍照,然后返回把所拍的照片显示出来
1、首先呢,我们先创建一个项目,然后修改一下布局文件activity_main.xml,ImageView是将拍照的图片显示出来:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/take_photo"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="take photo"
  13. />
  14. <ImageView
  15. android:id="@+id/picture"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_gravity="center_horizontal"/>
  19. </RelativeLayout>

然后开始处理调用摄像头的逻辑,编写MainActivity
File对象用来存放拍下的图片,GetUriForFile()方法接受3个参数。第一个是要求传入的Context对象,第二个是任意唯一的字符串,第三个是刚刚创建的File对象,调用Intent的putExtra()方法指定图片的输出地址,最后调用StartActivityForResult()来启动活动。

  1. package com.example.mrlee.camera;
  2. import android.content.Intent;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.provider.MediaStore;
  8. import android.support.annotation.Nullable;
  9. import android.support.v4.content.FileProvider;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.ImageView;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. public class MainActivity extends AppCompatActivity {
  19. public static final int TAKE_PHOTO = 1;
  20. private ImageView picture;
  21. private Uri imageUri;
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. Button takephoto = findViewById(R.id.take_photo);
  26. picture = findViewById(R.id.picture);
  27. takephoto.setOnClickListener(new View.OnClickListener() {
  28. public void onClick(View view) {
  29. File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
  30. try//判断图片是否存在,存在则删除在创建,不存在则直接创建
  31. {
  32. if (outputImage.exists()) {
  33. outputImage.delete();
  34. }
  35. outputImage.createNewFile();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. //判断运行设备的系统版本是否低于Android7.0
  40. if (Build.VERSION.SDK_INT >= 24)
  41. {
  42. imageUri = FileProvider.getUriForFile(MainActivity.this,
  43. "com.example.cameraalbumtest.fileprovider", outputImage);
  44. } else {
  45. imageUri = Uri.fromFile(outputImage);
  46. }
  47. //使用隐示的Intent,调用摄像头,并把它存储
  48. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  49. intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
  50. startActivityForResult(intent, TAKE_PHOTO);
  51. //调用会返回结果的开启方式,返回成功的话,则把它显示出来
  52. }
  53. });
  54. }
  55. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  56. switch (requestCode) {
  57. case TAKE_PHOTO:
  58. if (resultCode == RESULT_OK) {
  59. try {
  60. Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
  61. picture.setImageBitmap(bitmap);
  62. //将图片解析成Bitmap对象,并把它显现出来
  63. } catch (FileNotFoundException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. }

然后在AndroidMainfest.xml中队内容提供器进行注册

  1. <provider
  2. android:authorities="com.example.cameraalbumtest.fileprovider"
  3. android:name="android.support.v4.content.FileProvider"
  4. android:exported="false"
  5. android:grantUriPermissions="true">
  6. <meta-data
  7. android:name="android.support.FILE_PROVIDER_PATHS"
  8. android:resource="@xml/file_paths" />
  9. </provider>

@xml/file_paths资源还需要我们手动去创建,在res下创建一个xml目录,然后创建一个file_paths.xml文件, path的值表示共享的具体路劲,内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. <external-path
  4. name="my_images"
  5. path=""
  6. />
  7. </paths>

最后呢我们还需要访问SD卡的权限,在AndroidMainfest.xml中加一行代码

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

这样就搞定啦,点击拍照,它会将拍下来的图片返回,然后通过ImageView显示出来。

具体细节请参见《第一行代码》

发表评论

表情:
评论列表 (有 0 条评论,714人围观)

还没有评论,来说两句吧...

相关阅读