请求摄像机权限
为了声明应用程序依赖于摄像头,把一个<uses-feature>标签manifest文件中:
<manifest ... > <uses-feature android:name="android.hardware.camera" /> ... </manifest ... >
添加android:required="false"标签
通过调用hasSystemFeature(PackageManager.FEATURE_CAMERA)检查的摄像头在运行时的可用性
Android的行动委派到其他应用程序的方式是调用一个Intent,描述你想要做什么。这个过程包括三个部分:Intent本身,调用start外部的活动,和一些代码来处理图像数据时,焦点返回到你的活动。
这里有一个拍摄照片的Intent。
private void dispatchTakePictureIntent(int actionCode) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, actionCode); }
这里是一个函数来检查应用程序是否可以处理你的意图
public static boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
查看图片
onActivityResult()返回Intent中的照片缩略图。下面的代码检索这一形象,并将它显示在一个ImageView的。
private void handleSmallCameraPhoto(Intent intent) { Bundle extras = intent.getExtras(); mImageBitmap = (Bitmap) extras.get("data"); mImageView.setImageBitmap(mImageBitmap); }
保存图片
全尺寸的照片必须提供包括存储卷,文件夹和文件名的路径。
有一个简单的方式来获得照片的路径,但它只能在Android 2.2的(API8级)和更高版本:
storageDir = new File( Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ), getAlbumName() );
对于早期的API级别,你必须提供自己的照片目录的名称。
storageDir = new File ( Environment.getExternalStorageDirectory() + PICTURES_DIR + getAlbumName() );
注:路径组件PICTURES_DIR的仅仅是pictures/,外部共享存储上共享照片的标准位置。
设置文件名
需要做的是选择文件命名方案。也不妨在一个成员变量保存路径,以供日后使用。下面是一个示例解决方案:
private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_"; File image = File.createTempFile( imageFileName, JPEG_FILE_SUFFIX, getAlbumDir() ); mCurrentPhotoPath = image.getAbsolutePath(); return image; }
一旦你有一个地方来保存图像,通过Intent将该位置传递给摄像头应用程序。
File f = createImageFile(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
添加照片Gallery
当你通过一个Intent创建照片,你知道在哪里保存它。但对于其他人,让你的照片可从系统的媒体提供商访问,也许是最简单的方法
下面的例子方法演示了如何调用系统的媒体扫描仪添加您的照片到媒体提供商的数据库,使得它可以在Android 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); }
缩放图像解码
内存是有限的,管理多个全尺寸的图像可能会非常棘手。如果您发现您的应用程序运行几张图片就耗尽了内存,你可以显著降低用于扩大已经缩放匹配目标视图的JPEG到内存阵列动态堆使用量。下面的例子方法演示了这个技术。
private void setPic() { // Get the dimensions of the View int targetW = mImageView.getWidth(); int targetH = mImageView.getHeight(); // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min(photoW/targetW, photoH/targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); mImageView.setImageBitmap(bitmap); }