相机篇
Request Camera Permission
如果照相是你app的一个基本功能,那么你可以在GooglePlay上对安装设备做限制,使用下面的一个小片段即可完成:
<manifest ... > <uses-feature android:name="android.hardware.camera" android:required="true" /> ... </manifest>
如果你将required属性置为false,那么GooglePlay将会允许没有相机的设备安装你的应用,你就必须在运行时检查相机是否可用:
hasSystemFeature(PackageManager.FEATURE_CAMERA)
Take a Photo with the Camera App
按Android的规矩,委托另一个应用去干一件事,需要3个玩意:
The Intent
itself
call to start the external Activity
some code to handle the image data when focus returns to your activity
下面的片段代码,就是用intent打开相机拍照:
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); } }
为什么要用resolveActivity来进行检查?
这非常重要,resolveActivity()这个函数返回可以处理takePictureIntent 的第一个activity组件,如果没有activity可以处理这个intent,startActivityForResult()会crash。
Get the Thumbnail
Android的相机应用将一个小的Bitmap放在的Intent中返回,并且键值为data,我们可以按下面的片段取出缩略图,放在ImageView中显示:
@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); } }
Save the Full-size Photo
一般来说,相机拍出来的图片是应该被所有的应用共享的,所以应该放在外部储存设备中,下面的路径比较合适:
getExternalStoragePublicDirectory(DIRECTORY_PICTURES)
需要添加相应的权限:
<manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... </manifest>
如果你想让你的应用拍的照片私有化,你可以使用getExternalFilesDir()提供的目录,在android4.3及以下使用这个目录需要WRITE_EXTERNAL_STORAGE权限,
但是从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; }
开启相机的时候,将文件转为uri传递进去:
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); } } }
Add the Photo to a Gallery
当你把拍的照片保存到getExternalFilesDir()提供的目录下时,系统的媒体扫描时无法访问这个目录的,因为没有权限,所以如果你想让照片添加到系统的图库中,可以使用下面的代码:
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); }
Decode a Scaled Image
展示图片的时候,缩放处理:
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); }