Android应用内启动相机拍照并获取照片的两种方式
首先加入需要的权限,如果是6.0系统需要加权限申请
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
方式一:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//用于展示选择的图片
private ImageView mImageView;
private static final int CAMERA_CODE = 1;
private static final int GALLERY_CODE = 2;
private static final int CROP_CODE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mImageView = (ImageView) findViewById(R.id.show_image);
Button chooseCamera = (Button) findViewById(R.id.choose_camera);
chooseCamera.setOnClickListener(this);
Button chooseGallery = (Button) findViewById(R.id.choose_gallery);
chooseGallery.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.choose_camera:
//拍照选择
chooseFromCamera();
break;
case R.id.choose_gallery:
//从相册选取
chooseFromGallery();
break;
default:
break;
}
}
/**
* 拍照选择图片
*/
private void chooseFromCamera() {
//构建隐式Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//调用系统相机
startActivityForResult(intent, CAMERA_CODE);
}
/**
* 从相册选择图片
*/
private void chooseFromGallery() {
//构建一个内容选择的Intent
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//设置选择类型为图片类型
intent.setType("image/*");
//打开图片选择
startActivityForResult(intent, GALLERY_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_CODE:
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "取消了拍照", Toast.LENGTH_LONG).show();
return;
}
if(data != null){
Bitmap photo = data.getParcelableExtra("data");
//将Bitmap转化为uri
Uri uri = saveBitmap(photo, "temp");
//启动图像裁剪
startImageZoom(uri);
}
break;
case GALLERY_CODE:
if (data == null){
return;
}else{
//用户从图库选择图片后会返回所选图片的Uri
Uri uri;
//获取到用户所选图片的Uri
uri = data.getData();
//返回的Uri为content类型的Uri,不能进行复制等操作,需要转换为文件Uri
uri = convertUri(uri);
startImageZoom(uri);
}
break;
case CROP_CODE:
if (data == null){
return;
}else{
Bundle extras = data.getExtras();
if (extras != null){
//获取到裁剪后的图像
Bitmap bm = extras.getParcelable("data");
mImageView.setImageBitmap(bm);
}
}
break;
default:
break;
}
}
/**
* 将content类型的Uri转化为文件类型的Uri
* @param uri
* @return
*/
private Uri convertUri(Uri uri){
InputStream is;
try {
//Uri ----> InputStream
is = getContentResolver().openInputStream(uri);
//InputStream ----> Bitmap
Bitmap bm = BitmapFactory.decodeStream(is);
//关闭流
is.close();
return saveBitmap(bm, "temp");
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 将Bitmap写入SD卡中的一个文件中,并返回写入文件的Uri
* @param bm
* @param dirPath
* @return
*/
private Uri saveBitmap(Bitmap bm, String dirPath) {
//新建文件夹用于存放裁剪后的图片
File tmpDir = new File(Environment.getExternalStorageDirectory() + "/" + dirPath);
if (!tmpDir.exists()){
tmpDir.mkdir();
}
//新建文件存储裁剪后的图片
File img = new File(tmpDir.getAbsolutePath() + "/avator.png");
try {
//打开文件输出流
FileOutputStream fos = new FileOutputStream(img);
//将bitmap压缩后写入输出流(参数依次为图片格式、图片质量和输出流)
bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
//刷新输出流
fos.flush();
//关闭输出流
fos.close();
//返回File类型的Uri
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 通过Uri传递图像信息以供裁剪
* @param uri
*/
private void startImageZoom(Uri uri){
//构建隐式Intent来启动裁剪程序
Intent intent = new Intent("com.android.camera.action.CROP");
//设置数据uri和类型为图片类型
intent.setDataAndType(uri, "image/*");
//显示View为可裁剪的
intent.putExtra("crop", true);
//裁剪的宽高的比例为1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//输出图片的宽高均为150
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
//裁剪之后的数据是通过Intent返回
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_CODE);
}
}
注:第一种方式参考自冰鉴IT包括了拍照,裁剪和从相册选择图片,注释很详细.简单说明一下,通过startActivityForResult启动系统相机,然后在onActivityResult通过data.getParcelableExtra("data");方法获取到Bitmap对像,再通过IO保存到本地.
需要注意的是,这种方式获取到的图片是被系统压缩过的,优点是不容易OOM,适用于小图展示,如头像等.
下面介绍第二种,获取拍照的高清无码大图
方式二:
先指定拍照文件的保存路径Environment.ExternalStorageDirectory/eos/time.jpg,再通过 MediaStore.ACTION_IMAGE_CAPTURE 获取系统相机,并指定 MediaStore.EXTRA_OUTPUT ,的存储位置为我们刚刚指定路径的uri,intent.putExtra(MediaStore.EXTRA_OUTPUT,currentUri); 这样,最终返回的信息会存储在我们的file中。
private Uri currentUri;
private final int TakePhoto = 123;
private void openTakePhoto() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File appDir = new File(Environment.getExternalStorageDirectory(), "eos");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
file.createNewFile();
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
currentUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, currentUri);
startActivityForResult(intent, TakePhoto);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "文件存储路径异常", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "请插入sd卡", Toast.LENGTH_SHORT).show();
}
}
在onActivityResult进行相应的处理这里使用Glide通过之前的uri显示图片,如果是需要获取图片的file或bitmap则需要进行相应的压缩,防止发生OOM
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
// Toast.makeText(this, "取消了拍照", Toast.LENGTH_LONG).show();
return;
}
if (requestCode == TakePhoto && currentUri != null) {
photos.add(0, new PhotoBean(currentUri, 1));
adapter.notifyDataSetChanged();
}
}