Android呼叫系統相機
阿新 • • 發佈:2019-01-02
簡述:
Android開發中會經常遇到呼叫系統相機功能的場景,今天總結一下Camera的基本使用。
技術點:
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);
}
}