1、需要设置访问权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、点击拍照时判断是否获取手机拍照权限
public void takePhone(View view){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CALL_PHONE2);
}else{
takePhoto();
}
}
3、开始拍照
public void takePhoto(){
// 创建文件夹
File file = new File(Environment.getExternalStorageDirectory(),"my_photo") ;
if (!file.exists()){
file.mkdir();
}
// 将时间作为不同照片的名称
output = new File(file,System.currentTimeMillis()+".jpg");
// 如果该文件夹已经存在,则删除,重建一个
try {
if (output.exists()){
output.delete();
}
output.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
// 隐式打开拍照的activity,并传入CROP_PHOTO常量作为拍照结束后回调的标志
imageUri = Uri.fromFile(output);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,CROP_PHOT);
}
4、判断是否有访问相册权限
public void choosePhone(View view){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CALL_PHONE2);
}else {
choosePhoto();
}
}
5、选择相册中的照片
public void choosePhoto(){
// 打开选择相册的界面
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
6、获取照片后处理
public void onActivityResult(int req, int res, Intent data) {
switch (req) {
/**
* 拍照的请求标志
*/
case CROP_PHOT:
if (res==RESULT_OK) {
try {
/**
* 该uri就是照片文件夹对应的uri
*/
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bit);
} catch (Exception e) {
Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{
Log.i("tag", "失败");
}
break;
/**
* 从相册中选取图片的请求标志
*/
case REQUEST_CODE_PICK_IMAGE:
if (res == RESULT_OK) {
try {
/**
* 该uri是上一个Activity返回的
*/
Uri uri = data.getData();
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bit);
} catch (Exception e) {
e.printStackTrace();
Log.d("tag",e.getMessage());
Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{
Log.i("liang", "失败");
}
break;
default:
break;
}
}
7、获取权限result
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
takePhoto();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE2)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
choosePhoto();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
注:以上用的的变量
private ImageView imageView;
private static final int CROP_PHOT = 2;
private static final int REQUEST_CODE_PICK_IMAGE=3;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 6;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE2 = 7;
private File output;
private Uri imageUri;