1、说明,调用系统拍照是需要在有摄像头的设备上才能使用的功能,因此需要声明,可见android在最开始的时候还计划了没有带摄像头的设备。
<manifest ... > <uses-feature android:name="android.hardware.camera" android:required="true" /> </manifest>
可以使用PackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)检测android设备是否带有摄像头。
2、通过定义Intent的Action为MediaStore.ACTION_IMAGE_CAPTURE来启动可以相应此Action的程序,比如系统相机或者第三方照相程序。
static final int REQUEST_IMAGE_CAPTURE = 1; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } }
通过Intent.resolveActivity (PackageManager pm)判断是否有程序能够响应Action,如果不做此判断直接调用startActivityForResult,如果没有能够响应此Action的程序则会崩溃。
3、步骤2使用了startActivityForResult,那么需要重写onActivityResult
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); mImageView.setImageBitmap(imageBitmap); } }
默认的方式就是直接返回bitmap,通过此方式返回的图片大小做了限制,如果想返回高质量的图片,则需要参照步骤4;
4、调用系统拍照并且传递MediaStore.EXTRA_OUTPUT,那么图片将会保存到我们制定的路径当中。声明写入文件权限。
<manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
a、如果保存的图片是开放的,那么可以保存到Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)所返回的目录下,并且声明以上权限;
b、如果图片是程序私有的,那么可以保存到Context.getExternalFilesDir(String type)返回的目录下,如果是4.4版本及以上,则不必声明以上权限,否则需要声明
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
</manifest>
设置了图片保存目录如下
String mCurrentPhotoPath; private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; }
如果创建图片路径没有问题,则把此路径通过MediaStore.EXTRA_OUTPUT传递到Intent中
static final int REQUEST_TAKE_PHOTO = 1; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ... } // Continue only if the File was successfully created if (photoFile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } } }
在onActivityResult判断操作是否成功,成功之后则photoFile就是图片保存的路径。
5、如果需要将以上图片保存到Gallery(图库)中,则需要以下操作发送更新广播
private void galleryAddPic() { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); }