android intent拍照,Android拍照,选择照片

启动相机拍照

//用相机拍照获取图片

private void takePhoto() {

imgUri = HoYoImgHelper.getOutPutPicFileUri();

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

startActivityForResult(takePhotoIntent, TakePhotoRequestCode);

}

在onActivityResult()中处理返回结果

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

switch (requestCode) {

case TakePhotoRequestCode:

try {

showSelImage(imgUri.getPath());

} catch (Exception ex) {

ex.printStackTrace();

}

break;

}

}

}

imgUri获取方法:

//获取图片要保存的uri

public static Uri getOutPutPicFileUri() {

return Uri.fromFile(getOutPutPicFile());

}

//获取图片要保存的路径

public static File getOutPutPicFile() {

File mediaStorageDir = null;

try {

mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

} catch (Exception e) {

e.printStackTrace();

}

// Create the storage directory if it does not exist

if (!mediaStorageDir.exists()) {

if (!mediaStorageDir.mkdirs()) {

return null;

}

}

// Create a media file name

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

File mediaFile;

mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

return mediaFile;

}

选择图片

Intent selPicIntent = new Intent();

selPicIntent.setType("image/*");

selPicIntent.setAction(Intent.ACTION_PICK);

startActivityForResult(selPicIntent, SelPicRequestCode);

或者

Intent selPicIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

selPicIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

startActivityForResult(selPicIntent, SelPicRequestCode);

在onActivityResult()中处理返回结果

case SelPicRequestCode:

try {

Uri selectedImage = data.getData();

String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);

cursor.moveToFirst();

ContentResolver cr = this.getContentResolver();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

String picturePath = cursor.getString(columnIndex);

showSelImage(picturePath);

}catch (Exception ex) {

ex.printStackTrace();

}

break;

其中ShowSelImage(String imgPath)方法是根据图片绝对路径显示到指定控件上的方法

注:选择图片版本相关

Intent selPicIntent = new Intent();

selPicIntent.setType("image/*");

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {

selPicIntent.setAction(Intent.ACTION_PICK);

} else {

selPicIntent.setAction(Intent.ACTION_GET_CONTENT);

}

startActivityForResult(selPicIntent, SelPicRequestCode);

onActivityResult()中处理方法

String picturePath;

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {

String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage,

filePathColumn, null, null, null);

cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

picturePath = cursor.getString(columnIndex);

} else {

picturePath = selectedImage.getPath();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值