React-Native 中使用 react-native-image-crop-picker 在华为手机上不能正常使用拍照功能

背景:

React-Native 0.66 中使用 react-native-image-crop-picker 在安卓 华为手机上不能正常使用拍照功能, 其他品牌正常

代码如下:

复制代码
import ImagePicker from 'react-native-image-crop-picker';



ImagePicker.openCamera(photoOptions)
  .then(image => {
     callback(image);
  })

原因分析及解决办法:

通过在 openCamera中增加 catch 捕获异常发现, 在华为手机上调用拍照时有报错,

  1. Error: Cannot launch camera

poromiseMethodwrapper@http://localhost :8081/index .bundle?xxx

==> 在 android/app/src/main/AndroidManifest.xml 中增加 android:requestLegacyExternalStorage="true" 配置

  1. 再继续调试发现另外报错

Failed to find configured root that contains /storage/emulated/0/Android/data/com.jgpdapro/files/Pictures/image-4488cf2d-8a66-4254-a9d6-f198bb1515781214910780677416114.jpg

promiseMethodWrapper@http://localhost:8081/index.bundle?platform=android\&dev=true\&minify=false\&app=com.jgpdapro\&modulesOnly=false\&runModule=true:2084:45

http://localhost:8081/index.bundle?platform=android\&dev=true\&minify=false\&app=com.jgpdapro\&modulesOnly=false\&runModule=true:171700:57

==> 在 android/app/src/main/AndroidManifest.xml 中增加 android.support.FILE_PROVIDER_PATHS 配置

复制代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.xxx">


    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:usesCleartextTraffic="true"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:requestLegacyExternalStorage="true" 
      android:theme="@style/AppTheme">
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
</manifest>

在 android/app/src/main/res/xml/下新增 file_paths.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_pictures"
        path="Android/data/com.xxx/files/Pictures/" />
</paths>

测试结果:

相关推荐
前端技术6 分钟前
通信网络基础(下篇):TCP/IP网络参考模型与传输层协议深度解析
华为
kyriewen14 分钟前
React Hooks原理:为什么不能写在if里?揭开Hook的“魔法”面纱
前端·react.js·前端框架
IntMainJhy4 小时前
Flutter 三方库 ImageCropper 图片裁剪鸿蒙化适配与实战指南(正方形+自定义比例全覆盖)
flutter·华为·harmonyos
IntMainJhy4 小时前
Flutter for OpenHarmony 第三方库六大核心模块整合实战全解|从图片处理、消息通知到加密存储、设备推送 一站式鸿蒙适配开发总结
flutter·华为·harmonyos
UnicornDev4 小时前
【HarmonyOS 6】基于API23的底部悬浮导航
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
音视频牛哥4 小时前
鸿蒙 NEXT 时代:如何构建工业级稳定和低延迟的同屏推流模块?
华为·harmonyos·大牛直播sdk·鸿蒙next 无纸化同屏·鸿蒙next同屏推流·鸿蒙rtmp同屏·鸿蒙无纸化会议同屏
IntMainJhy4 小时前
【fluttter for open harmony】Flutter 三方库适配实战:在 OpenHarmony 上实现图片压缩功能(附超详细踩坑记录)
flutter·华为·harmonyos
jiejiejiejie_4 小时前
Flutter for OpenHarmony 多语言国际化超简单实现指南
flutter·华为·harmonyos
2301_814809865 小时前
【HarmonyOS 6.0】ArkWeb 嵌套滚动快速调度策略:从机制到落地的全景解析
华为·harmonyos
南村群童欺我老无力.5 小时前
鸿蒙中AppStorage全局状态管理的生命周期问题
华为·harmonyos