Android 拍照 和 从相册获取图片
拍照:
photoAbsoluteFile = new File(createImagePath());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用系统自带照相机
Uri photoUri = Uri.fromFile(photoAbsoluteFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 设置图像的Uri存储地址
startActivityForResult(intent, LibConstants.REQUEST_CODE_LAUNCHCAMERA);
从相册取图片:
Intent albumIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
albumIntent.setType("image/*");
startActivityForResult(albumIntent, LibConstants.REQUEST_CODE_LAUNCHALBUM);
拍完照片或者从相册选完照片后获取图片地址:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case LibConstants.REQUEST_CODE_LAUNCHCAMERA: // 相机
if (photoAbsoluteFile != null)
mFiles.add(photoAbsoluteFile.getAbsolutePath());////photoAbsoluteFile 这个是拍照时传的路径
break;
case LibConstants.REQUEST_CODE_LAUNCHALBUM: // 相册
Uri uri = data.getData();
if (uri != null) {
String[] proj = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, proj, null, null, null);
int index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
// 最后根据索引值获取图片路径
mFiles.add(cursor.getString(index));////cursor.getString(index) 这个就是图片的绝对地址了
}
break;
}
}