普通的相机调用,在 intent 传进去一个路径,然调用这个意图。java
在测试机 荣耀 8X 上是没有问题的,能获取到拍的照片。ide
在小米系统和 华为麦芒4上就不行,路径上就没有照片。测试
/** * @param file 拍照生成的照片地址 * @return intent */ public static Intent getTakePictureIntent(File file) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(CommonUtils.context.getPackageManager()) != null) { if (null != file) { tempPicturePath = file.getPath(); LogUtil.log(DAUtils.class.getSimpleName(),"getTakePictureIntent :->>"+tempPicturePath); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } else { ContentValues contentValues = new ContentValues(1); contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); // Uri uri = CommonUtils.context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); Uri uri = FileProvider.getUriForFile(CommonUtils.context, "xxx.fileProvider", file.getAbsoluteFile()); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } } } return intent; }
不能获取到照片的缘由是由于这个照片的目录没有建立。ui
在传入 URI 以前要把照片的目录给建立出来。否则就拿不到照片。google
修改以下:在传入照片 URI 前保证目录已经被建立debug
/** * @param file 拍照生成的照片地址 * @return intent */ public static Intent getTakePictureIntent(File file) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(CommonUtils.context.getPackageManager()) != null) { if (null != file) { if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } tempPicturePath = file.getPath(); LogUtil.log(DAUtils.class.getSimpleName(),"getTakePictureIntent :->>"+tempPicturePath); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } else { ContentValues contentValues = new ContentValues(1); contentValues.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); // Uri uri = CommonUtils.context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); Uri uri = FileProvider.getUriForFile(CommonUtils.context, "xxx.fileProvider", file.getAbsoluteFile()); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } } } return intent; }
反复 debug 断点,google 半小时无果后,灵光一闪,想到了。code
这是个坑get
Endit