zoukankan      html  css  js  c++  java
  • 适配小米华为手机等拍照后获取不到照片

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

    出现场景

    普通的相机调用,在 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

  • 相关阅读:
    final方法修饰继承问题
    日期比较
    子类继承父类,初始化以及方法调用顺序
    linux机器上实现占用固定cpu使用率,并将程序绑定到固定cpu上
    【译】Rust宏:教程与示例(二)
    【译】Rust宏:教程与示例(一)
    【译】Async/Await(五)—— Executors and Wakers
    【译】Async/Await(四)—— Pinning
    通过工具dumpbin.exe快速查看dll的编译版本是Release还是Debug
    【常用操作】ubuntu部署说明
  • 原文地址:https://www.cnblogs.com/skymxc/p/12085524.html
Copyright © 2011-2022 走看看