普通的相機調用,在 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