赞
踩
1.打开相机的Intent Action: MediaStore.ACTION_IMAGE_CAPTURE,下面为它的注释:
- /**
- * Standard Intent action that can be sent to have the camera application
- * capture an image and return it.
- * <p>
- * The caller may pass an extra EXTRA_OUTPUT to control where this image will be written.
- * If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap
- * object in the extra field. This is useful for applications that only need a small image.
- * If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri
- * value of EXTRA_OUTPUT.
- * As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this uri can also be supplied through
- * {@link android.content.Intent#setClipData(ClipData)}. If using this approach, you still must
- * supply the uri through the EXTRA_OUTPUT field for compatibility with old applications.
- * If you don't set a ClipData, it will be copied there for you when calling
- * {@link Context#startActivity(Intent)}.
- *
- * <p>Note: if you app targets {@link android.os.Build.VERSION_CODES#M M} and above
- * and declares as using the {@link android.Manifest.permission#CAMERA} permission which
- * is not granted, then atempting to use this action will result in a {@link
- * java.lang.SecurityException}.
- *
- * @see #EXTRA_OUTPUT
- */
- @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
- public final static String ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE";

从注释可以知道,该Action会打开照相机拍照然后返回结果,如果在没有设置额外的控制图片存储路径的参数MediaStore.EXTRA_OUTPUT情况下,返回的结果将是一个小的Bitmap,这仅仅只适合于应用程序需要一张小的图片的时候;如果设置了MediaStore.EXTRA_OUTPUT,那么将保存整个大小的到指定的Uri中。
没有设置MediaStore.EXTRA_OUTPUT的情况下:
- public void onTakePhotoClick(View view) {
- Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
- startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
- }
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
- Bundle extras = data.getExtras();
- mImageBitmap = (Bitmap) extras.get("data");
- mThumbView.setImageBitmap(mImageBitmap);
- }
- }
从(Bitmap) extras.get("data")直接就可以获取到返回的图片,但这种图片小且质量很差,下面为测试的返回结果:
设置MediaStore.EXTRA_OUTPUT的情况下,需要添加
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/mnt/sdcard/DCIM/Album/1.jpg")));
附加:/mnt/sdcard/DCIM/Album/1.jpg为图片的存储地址。
2.将图片添加到图库中
这样拍的照片并不会出现在系统的图库中,但经过一些测试,有些手机关机重启之后,相册中会出现这些照片,原因是重启之后系统扫描了文件夹,将图片添加到了多媒体数据库中。下面为将图片添加到多媒体数据中的方法:
2.1通过发送广播给系统将文件添加到多媒体数据库中:
- /*
- * Request the media scanner to scan a file and add it to the media database.
- */
- private void scanFile() {
- Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
- intent.setData(Uri.fromFile(new File("/mnt/sdcard/DCIM/Album/1.jpg")));
- sendBroadcast(intent);
- }
这段代码会启动一个叫MediaScannerBroadcast广播,但该广播并不会去扫描文件,是交给一个叫MediaScannerService服务去完成的,另外上面的Uri也可以换成文件夹的路径。
2.2手动将图片插入到媒体数据中:
Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/Album/1.jpg");
String stringUrl = MediaStore.Images.Media.insertImage(getContentResolver(), bm, "", "");
下面为insertImage(ContentResolver cr, String imagePath,String name, String description)的源码:
- <span style="font-size:18px;"> /**
- * Insert an image and create a thumbnail for it.
- *
- * @param cr The content resolver to use
- * @param source The stream to use for the image
- * @param title The name of the image
- * @param description The description of the image
- * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored
- * for any reason.
- */
- public static final String insertImage(ContentResolver cr, Bitmap source,
- String title, String description) {
- ContentValues values = new ContentValues();
- values.put(Images.Media.TITLE, title);
- values.put(Images.Media.DESCRIPTION, description);
- values.put(Images.Media.MIME_TYPE, "image/jpeg");
-
- Uri url = null;
- String stringUrl = null; /* value to be returned */
-
- try {
- url = cr.insert(EXTERNAL_CONTENT_URI, values);
-
- if (source != null) {
- OutputStream imageOut = cr.openOutputStream(url);
- try {
- source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
- } finally {
- imageOut.close();
- }
-
- long id = ContentUris.parseId(url);
- // Wait until MINI_KIND thumbnail is generated.
- Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id,
- Images.Thumbnails.MINI_KIND, null);
- // This is for backward compatibility.
- Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
- Images.Thumbnails.MICRO_KIND);
- } else {
- Log.e(TAG, "Failed to create thumbnail, removing original");
- cr.delete(url, null, null);
- url = null;
- }
- } catch (Exception e) {
- Log.e(TAG, "Failed to insert image", e);
- if (url != null) {
- cr.delete(url, null, null);
- url = null;
- }
- }
-
- if (url != null) {
- stringUrl = url.toString();
- }
-
- return stringUrl;
- }</span>

从源码可以知道,其实是通过多媒体的内容提供者将图片插入到了数据库中,另外将图片插入到多媒体数据库中之后,我们需要获取图片,通过返回结果stringUrl 可以知道,它就是图片存储在媒体数据库中的路径,因此:
String path = getPath(Uri.parse(stringUrl), "date_added desc limit 1");
- /**
- * 获得媒体数据库中图片的路径
- */
- private String getPath(Uri uri, String sortOrder) {
- String path = null;
- Cursor cursor = getContentResolver().query(uri, null, null, null, sortOrder);
- if (cursor != null) {
- if (cursor.getCount() > 0) {
- while (cursor.moveToNext()) {
- path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
- }
- }
- cursor.close();
- }
- return path;
- }

另外谢谢解决Android拍照保存在系统相册不显示的问题这篇博客提供的帮助。
附加:系统相册的存储路径为文件夹"/mnt/sdcard/DCIM/Camera"下,如果拍照之后,系统图库不显示拍的照片,可以将图片存储的路径改为文件夹"/mnt/sdcard/DCIM/Camera"下,经过测试,一定会显示到系统图库中。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。