• 适配小米华为手机等拍照后获取不到照片


    问题摘要:适配小米华为手机等拍照后获取不到照片

    出现场景

    普通的相机调用,在 intent 传进去一个路径,然调用这个意图。

    在测试机 荣耀 8X 上是没有问题的,能获取到拍的照片。

    在小米系统和 华为麦芒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;
        }
    
    

    出现原因

    不能获取到照片的原因是因为这个照片的目录没有创建。

    在传入 URI 之前要把照片的目录给创建出来。不然就拿不到照片。

    如何修复

    修改如下:在传入照片 URI 前保证目录已经被创建

       /**
         * @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 半小时无果后,灵光一闪,想到了。

    总结

    这是个坑

    End

  • 相关阅读:
    强烈推荐:240多个jQuery插件【转】
    逆变与协变详解
    Mac入门 (二) 使用VMware Fusion虚拟机
    JQUERY UI DOWNLOAD
    几个常用Json组件的性能测试
    jQuery.extend 函数详解
    获取Portal中POWL程序的APPLID
    设计师和开发人员更快完成工作需求的20个惊人的jqury插件教程(上)
    Linux kernel中网络设备的管理
    mongodb修改器
  • 原文地址:https://www.cnblogs.com/skymxc/p/12085524.html
Copyright © 2020-2023  润新知