Android调用系统相机

时间:2023-02-07 20:31:10

简述:

  Android开发中会经常遇到调用系统相机功能的场景,今天总结一下Camera的基本使用。

                                                    Android调用系统相机

技术点:

    1、首先要考虑权限问题,Android6.0以上的系统需要动态申请权限,实现方式如下:

    /**
* 方法描述:针对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();
}

    2、其次通过重写onRequestPermissionsResult()方法处理权限申请结果。

       

    @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;
}

}

}
}

3、其次是系统相机的调用及其拍照结果的回调。

    /**
* 方法描述:启动系统相机
*/
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;
}

}

}
}
}

PhotoShowActivity代码:

/**
* 类描述:
* 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);
}
}

完整源码下载