Android中调用相机拍照获取图片可通过两种方法获得,首先都通过调用系统服务来开启相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,requestCode);
第一种方法,通过startActivityForResult返回的结果中获取图片,如下:if (resultCode==RESULT_OK)
{ if (requestCode==REQ_1) { Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap)bundle.get("data"); iv.setImageBitmap(bitmap); }}但通过此方法获取的图片是经过压缩的,显示起来较模糊。第二种方法,在调用服务之前,设置拍照图片的保存路径,
path = Environment.getExternalStorageDirectory().getPath();path += "/"+"temp.png";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Uri photoUri = Uri.fromFile(new File(path));intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);startActivityForResult(intent,REQ_2);
在onActivityResult中读取图片,此情况下得到的图片是未经过压缩的
if (requestCode==REQ_2){ FileInputStream fis = null; try { fis = new FileInputStream(new File(path)); Bitmap bitmap = BitmapFactory.decodeStream(fis); iv.setImageBitmap(bitmap); }catch (Exception e) { e.printStackTrace(); }finally { try { if (fis!=null) fis.close(); }catch (Exception e) { e.printStackTrace(); } }}
时间: 2024-10-23 05:35:05