After adaptation millet Huawei mobile phones and other camera can not get photos

Problem Summary: After the adaptation millet Huawei mobile phones and other camera can not get photos

Appearance scene

Call ordinary camera, the intent pass in a path, and then call this intention.

8X glory on the test machine is no problem, you can get to the photos.

It will not work in the millet system and Huawei-head 4, there is no photo on the path.

   /**
     * @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;
    }

The reason

The reason can not get to photograph because of this photograph did not create directory.

Prior to take photos of the incoming URI directory to create it. Otherwise, you'll get the picture.

How to fix

Modified as follows: URI before passing the photo to ensure that the directory has been created

   /**
     * @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;
    }

Investigation process

Repeatedly debug breakpoint, after half an hour to no avail google, flash, thought.

to sum up

This is a pit

End

Guess you like

Origin www.cnblogs.com/skymxc/p/12085524.html