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>

测试结果:

相关推荐
Highcharts.js13 小时前
Highcharts Orbit 完全指南:把每一张图表变成交互式分析工具
react.js·前端框架·highcharts·orbit·使用指南·ai 图表·交互式分析工具
KevinChaos20 小时前
阳台小花园开发手记 03|HarmonyOS 7.0 互动卡片:叶片伸出边框仍与花盆对齐
华为·harmonyos
鸿蒙开发20 小时前
小程序存档写到一半怎么办?双槽保存与损坏恢复实战
前端·javascript·react.js
熬夜磕代码丶21 小时前
RN鸿蒙空间音频实战
华为·音视频·harmonyos
行者-全栈开发1 天前
【鸿蒙心迹】通知与推送集成——emitter 本地通知与推送服务全链路(HarmonyOS 7.x)
华为·harmonyos
Nerd(BUG版)1 天前
鸿蒙API18~26新增V2组件全解析
microsoft·华为·harmonyos
Felven1 天前
华为鲲鹏920s芯片serdestool工具使用说明
linux·运维·服务器·华为·鲲鹏920·serdestool
c2892480843s1 天前
【鸿蒙心迹】网络请求封装实战:RCP 选型、拦截器与弱网预算
移动开发·网络请求·harmonyos·鸿蒙·rcp
SoFarAway2 天前
拆解华为一碰传
华为·逆向
钉先生贾2 天前
毕业项目:从上游更新到回归发布的完整维护闭环
华为·harmonyos