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>

测试结果:

相关推荐
一直在学习的小白~4 小时前
中间添加一条可以拖拽的分界线,来动态调整两个模块的宽度
前端·javascript·react.js
Python私教4 小时前
鸿蒙 OS 开发零基础快速入门教程
android·华为·harmonyos
爱桥代码的程序媛4 小时前
鸿蒙OpenHarmony【轻量系统内核(异常调测)】子系统开发
嵌入式硬件·内核·harmonyos·鸿蒙·openharmony·鸿蒙开发·子系统开发
让开,我要吃人了4 小时前
HarmonyOS鸿蒙开发实战( Beta5.0)Web组件预览PDF文件实现案例
开发语言·华为·程序员·移动开发·harmonyos·鸿蒙·鸿蒙系统
凭栏落花侧5 小时前
现代前端框架实战指南:React、Vue.js、Angular核心概念与应用
前端·vue.js·经验分享·笔记·学习·react.js·前端框架
小白小白从不日白5 小时前
react hooks--useContext
前端·javascript·react.js
青瓷看世界17 小时前
华为HarmonyOS灵活高效的消息推送服务(Push Kit) - 3 获取AAID
华为·harmonyos
小白小白从不日白17 小时前
react hooks--useMemo
前端·javascript·react.js
资深前端之路17 小时前
react 创建react项目
前端·javascript·react.js