本文演示如何使用Intent调用Android系统自身的相机功能完成拍照。
本例聚焦在相机功能,通过相机拍照以后显示在ImageView中,至于如何保存在文件中或是相册中,将通过另外的文章来说明。
声明权限
使用相机,需要在应用的 AndroidManifest.xml 加入权限声明:
1
| <uses-permission android:name="android.permission.CAMERA"></uses-permission>
|
在程序中检查权限
在 Android 6 以后,需要在程序中检查用户是否授予应用权限
1 2 3 4 5
| private void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERSSION_CAMERA); } }
|
调用相机
使用 MediaStore.ACTION_IMAGE_CAPTURE 作为 action, 构造 Intent,并调用
1 2
| Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQ_CODE_IMAGE_CAPTURE);
|
处理Intent的返回
在Intent中,获取图像数据并简单的加载到屏幕中的 ImageView 对象中显示。
1 2 3 4 5 6 7 8 9 10 11 12
| @Override protected void onActivityResult(int reqCode, int resCode, Intent data) {
if (reqCode == REQ_CODE_IMAGE_CAPTURE && resCode == RESULT_OK) { Bitmap bitmap = (Bitmap)data.getExtras().get("data"); ivwPhoto.setImageBitmap(bitmap); } else if (reqCode == REQ_CODE_PERSSION_CAMERA ) { Log.d(TAG, "resCode = " + resCode); }
super.onActivityResult(reqCode, resCode, data); }
|
完整源代码
- AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.com.hohistar.tutorial.camerawithintentbasic">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
|
- activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/btnTakePhoto" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/btnTakePhoto" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<ImageView android:id="@+id/ivwPhoto" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnTakePhoto" app:srcCompat="@drawable/ic_launcher_background" /> </androidx.constraintlayout.widget.ConstraintLayout>
|
- MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public class MainActivity extends AppCompatActivity {
private static final String TAG = "MyTAG";
private static final int REQ_CODE_PERSSION_CAMERA = 1000; private static final int REQ_CODE_IMAGE_CAPTURE = 1001;
@BindView(R.id.ivwPhoto) protected ImageView ivwPhoto;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
ButterKnife.bind(this);
checkPermission(); }
private void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERSSION_CAMERA); } }
@OnClick(R.id.btnTakePhoto) void onBtnTakePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQ_CODE_IMAGE_CAPTURE); }
@Override protected void onActivityResult(int reqCode, int resCode, Intent data) {
if (reqCode == REQ_CODE_IMAGE_CAPTURE && resCode == RESULT_OK) { Bitmap bitmap = (Bitmap)data.getExtras().get("data"); ivwPhoto.setImageBitmap(bitmap); } else if (reqCode == REQ_CODE_PERSSION_CAMERA ) { Log.d(TAG, "resCode = " + resCode); }
super.onActivityResult(reqCode, resCode, data); }
}
|