当前位置:   article > 正文

Android 调用系统相机拍照的返回结果_华为适配 开发 调用系统相机拍视频返回

华为适配 开发 调用系统相机拍视频返回

  1.打开相机的Intent Action: MediaStore.ACTION_IMAGE_CAPTURE,下面为它的注释:

  1. /**
  2. * Standard Intent action that can be sent to have the camera application
  3. * capture an image and return it.
  4. * <p>
  5. * The caller may pass an extra EXTRA_OUTPUT to control where this image will be written.
  6. * If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap
  7. * object in the extra field. This is useful for applications that only need a small image.
  8. * If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri
  9. * value of EXTRA_OUTPUT.
  10. * As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this uri can also be supplied through
  11. * {@link android.content.Intent#setClipData(ClipData)}. If using this approach, you still must
  12. * supply the uri through the EXTRA_OUTPUT field for compatibility with old applications.
  13. * If you don't set a ClipData, it will be copied there for you when calling
  14. * {@link Context#startActivity(Intent)}.
  15. *
  16. * <p>Note: if you app targets {@link android.os.Build.VERSION_CODES#M M} and above
  17. * and declares as using the {@link android.Manifest.permission#CAMERA} permission which
  18. * is not granted, then atempting to use this action will result in a {@link
  19. * java.lang.SecurityException}.
  20. *
  21. * @see #EXTRA_OUTPUT
  22. */
  23. @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
  24. public final static String ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE";

  从注释可以知道,该Action会打开照相机拍照然后返回结果,如果在没有设置额外的控制图片存储路径的参数MediaStore.EXTRA_OUTPUT情况下,返回的结果将是一个小的Bitmap,这仅仅只适合于应用程序需要一张小的图片的时候;如果设置了MediaStore.EXTRA_OUTPUT,那么将保存整个大小的到指定的Uri中。

  没有设置MediaStore.EXTRA_OUTPUT的情况下:

  1. public void onTakePhotoClick(View view) {
  2. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  3. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  4. startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
  5. }
  6. }
  7. @Override
  8. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  9. if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
  10. Bundle extras = data.getExtras();
  11. mImageBitmap = (Bitmap) extras.get("data");
  12. mThumbView.setImageBitmap(mImageBitmap);
  13. }
  14. }
从(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通过发送广播给系统将文件添加到多媒体数据库中

  1. /*
  2. * Request the media scanner to scan a file and add it to the media database.
  3. */
  4. private void scanFile() {
  5. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  6. intent.setData(Uri.fromFile(new File("/mnt/sdcard/DCIM/Album/1.jpg")));
  7. sendBroadcast(intent);
  8. }
这段代码会启动一个叫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)的源码:

  1. <span style="font-size:18px;"> /**
  2. * Insert an image and create a thumbnail for it.
  3. *
  4. * @param cr The content resolver to use
  5. * @param source The stream to use for the image
  6. * @param title The name of the image
  7. * @param description The description of the image
  8. * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored
  9. * for any reason.
  10. */
  11. public static final String insertImage(ContentResolver cr, Bitmap source,
  12. String title, String description) {
  13. ContentValues values = new ContentValues();
  14. values.put(Images.Media.TITLE, title);
  15. values.put(Images.Media.DESCRIPTION, description);
  16. values.put(Images.Media.MIME_TYPE, "image/jpeg");
  17. Uri url = null;
  18. String stringUrl = null; /* value to be returned */
  19. try {
  20. url = cr.insert(EXTERNAL_CONTENT_URI, values);
  21. if (source != null) {
  22. OutputStream imageOut = cr.openOutputStream(url);
  23. try {
  24. source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
  25. } finally {
  26. imageOut.close();
  27. }
  28. long id = ContentUris.parseId(url);
  29. // Wait until MINI_KIND thumbnail is generated.
  30. Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id,
  31. Images.Thumbnails.MINI_KIND, null);
  32. // This is for backward compatibility.
  33. Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
  34. Images.Thumbnails.MICRO_KIND);
  35. } else {
  36. Log.e(TAG, "Failed to create thumbnail, removing original");
  37. cr.delete(url, null, null);
  38. url = null;
  39. }
  40. } catch (Exception e) {
  41. Log.e(TAG, "Failed to insert image", e);
  42. if (url != null) {
  43. cr.delete(url, null, null);
  44. url = null;
  45. }
  46. }
  47. if (url != null) {
  48. stringUrl = url.toString();
  49. }
  50. return stringUrl;
  51. }</span>
从源码可以知道,其实是通过多媒体的内容提供者将图片插入到了数据库中,另外将图片插入到多媒体数据库中之后,我们需要获取图片,通过返回结果stringUrl 可以知道,它就是图片存储在媒体数据库中的路径,因此:

String path = getPath(Uri.parse(stringUrl), "date_added desc limit 1");

  1. /**
  2. * 获得媒体数据库中图片的路径
  3. */
  4. private String getPath(Uri uri, String sortOrder) {
  5. String path = null;
  6. Cursor cursor = getContentResolver().query(uri, null, null, null, sortOrder);
  7. if (cursor != null) {
  8. if (cursor.getCount() > 0) {
  9. while (cursor.moveToNext()) {
  10. path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
  11. }
  12. }
  13. cursor.close();
  14. }
  15. return path;
  16. }

  OK,从上面解决了系统图库不显示应用程序拍照的图片得问题。

  另外谢谢解决Android拍照保存在系统相册不显示的问题这篇博客提供的帮助。

  附加:系统相册的存储路径为文件夹"/mnt/sdcard/DCIM/Camera"下,如果拍照之后,系统图库不显示拍的照片,可以将图片存储的路径改为文件夹"/mnt/sdcard/DCIM/Camera"下,经过测试,一定会显示到系统图库中。



本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/969995
推荐阅读
相关标签
  

闽ICP备14008679号