赞
踩
Android开发中会经常遇到调用系统相机功能的场景,今天总结一下Camera的基本使用。
- /**
- * 方法描述:针对Android 6.0以上系统的动态权限申请
- */
- private void requestCameraPermission() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CODE);
- return;
- }
-
- launchCamera();
- }
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (MY_PERMISSIONS_REQUEST_CODE == requestCode) { //循环遍历已经申请到的权限 for (int index = 0; index < permissions.length; index++) { switch (permissions[index]) { case Manifest.permission.CAMERA: //用户授权 if (PackageManager.PERMISSION_GRANTED == grantResults[index]) { launchCamera(); } else if (PackageManager.PERMISSION_DENIED == grantResults[index]) { Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权", Toast.LENGTH_SHORT).show(); } break; } } } }
- /**
- * 方法描述:启动系统相机
- */
- private void launchCamera() {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, REQUEST_CODE);
- }
至于结果回调还是重写onActivityResult()方法。
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == Activity.RESULT_OK) {
- Intent intent = new Intent(CameraActivity.this, PhotoShowActivity.class);
- intent.putExtra("picture", data.getExtras());
- startActivity(intent);
- } else {
- launchCamera();
- }
- }
CameraActivity代码:
package com.lizhenya.systemcamera; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.view.Window; import android.widget.Toast; public class CameraActivity extends Activity { private static final int REQUEST_CODE = 1; private static final int MY_PERMISSIONS_REQUEST_CODE = 2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); requestCameraPermission(); } /** * 方法描述:启动系统相机 */ private void launchCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CODE); } /** * 方法描述:针对Android 6.0以上系统的动态权限申请 */ private void requestCameraPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CODE); return; } launchCamera(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { Intent intent = new Intent(CameraActivity.this, PhotoShowActivity.class); intent.putExtra("picture", data.getExtras()); startActivity(intent); } else { launchCamera(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (MY_PERMISSIONS_REQUEST_CODE == requestCode) { //循环遍历已经申请到的权限 for (int index = 0; index < permissions.length; index++) { switch (permissions[index]) { case Manifest.permission.CAMERA: if (PackageManager.PERMISSION_GRANTED == grantResults[index]) { launchCamera(); } else if (PackageManager.PERMISSION_DENIED == grantResults[index]) { Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(CameraActivity.this, "应用没有拍照权限,请授权", Toast.LENGTH_SHORT).show(); } break; } } } } }
/** * 类描述: * Created by lizhenya on 16/8/23. */ public class PhotoShowActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); final Intent intent = getIntent(); final Bundle bundle = intent.getBundleExtra("picture"); Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式 ((ImageView)findViewById(R.id.iv)).setImageBitmap(bitmap); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。