No photos can be obtained after photos are taken, such as Xiaomi Huawei mobile phone

Posted by chrisdarby on Mon, 23 Dec 2019 22:34:12 +0100

Problem Abstract: no photos can be obtained after photos are taken with Xiaomi Huawei mobile phone

Scene appears

Ordinary camera call, pass in a path in intent, and call this intention.

There is no problem in the glory 8X of the test machine. You can get the photos.

In Xiaomi system and Huawei's maimang 4, there are no photos on the path.

   /**
     * @param file Photo address generated by taking photos
     * @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;
    }

Reasons for appearance

The reason why we can't get the photo is that the photo directory isn't created.

Before passing in the URI, create the photo directory. Or you won't get the picture.

How to repair

Modify as follows: make sure the directory has been created before passing in the photo URI

   /**
     * @param file Photo address generated by taking photos
     * @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;
    }

Troubleshooting process

Repeatedly debug breakpoints, google half an hour after fruitless, a flash of inspiration, thought of.

summary

This is a pit.

End

Topics: Android FileProvider Mobile Google