Problem Summary: No photos can be obtained after photos are taken on xiaomi and Huawei mobile phones

A scenario

Normal camera calls pass a path in the intent and then call the intent.

There is no problem on the honor 8X test machine, you can get the photos.

Not on the Mi system and Huawei Maimang 4, there are no photos on the path.

   / * * *@paramFile Address of the photo generated by the photo *@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;
    }

Copy the code

A reason for

The photo cannot be retrieved because the directory for the photo was not created.

Create the photo’s directory before passing in the URI. Or we won’t get the photos.

How to repair

Make sure the directory is created before passing in the photo URI

   / * * *@paramFile Address of the photo generated by the photo *@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;
    }

Copy the code

The screening process

Repeatedly debug breakpoints, Google half an hour after no results, a flash of inspiration, thought of.

conclusion

It’s a pit

End