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>

测试结果:

相关推荐
Magic-ZYJ5 小时前
HarmonyOS Release 构建安全:ArkGuard 混淆、签名配置与秘密管理
安全·华为·harmonyos·移动应用开发·心晴手记
见山是山-见水是水6 小时前
拆解HarmonyOS网络请求适配:原生鸿蒙页面的实现路径与调试方法
华为·harmonyos
Shinner欣儿7 小时前
React18 和 19 新特性结合看
前端·react.js
lqj_本人8 小时前
鸿蒙跨端实战_QT后台切换导致OpenGLES上下文丢失:黑屏自救指南
qt·华为·harmonyos
光影少年8 小时前
react navite环境与工程化
前端·react native·react.js
OH_TPC9 小时前
HarmonyOS APP开发---“数据眼“健康追踪App,需要用到这个库
华为·harmonyos·鸿蒙
用户2986985301410 小时前
使用 JavaScript 在 React 中实现 Word 转 PDF
前端·javascript·react.js
lilian23310 小时前
Harmony os 技术实战|拼豆制图49:五组分类视觉规则如何收口成一张配置表
华为·harmonyos
2501_9197490311 小时前
华为鸿蒙免费决策APP—小羊挠头
华为·harmonyos·鸿蒙
m0_7496902311 小时前
【寻迹校园 HarmonyOS NEXT 实战 37】一套 ArkUI 适配四档宽度:sm、md、lg、xl 响应式 Shell 实战
华为·harmonyos·arkui·navigation·响应式布局·多设备适配