【问题标题】:React native: image-crop-picker crash app AndroidReact native:image-crop-picker crash app Android
【发布时间】:2021-09-08 14:02:05
【问题描述】:

我使用 react-native-fs 从服务器下载图像,然后使用 react-native-image-crop-picker 中的 openCropper() 进行裁剪。它在 iOS 中运行良好,但在 Android 运行时崩溃,没有任何错误消息或警报。

你有过这个问题吗?给我一个指导。谢谢大家。

 const onCropImage = async () => {
    setViewImageModal(false);

    const uri = `${RNFS.DocumentDirectoryPath}/${fileName}`;
    let options = {
      fromUrl: viewImage && viewImage[0].url,
      toFile: uri,
    };

    await RNFS.downloadFile(options).promise;

// App crashed when I call .openCropper() in Android running
    ImageCropPicker.openCropper({
      path: uri,
      width: 300,
      height: 400,
      cropping: true,
      freeStyleCropEnabled: true,
    })
      .then((image) => {
        if (image) {
          const temp = image.path.split("/");
          const imageName = temp[temp.length - 1];
          navigation.navigate("EditHostScreen", {
            host,
            type: "hosts",
            info: { uri: image.path, typeImage: image.mime, name: imageName },
          });

          const time = setTimeout(() => {
            ImageCropPicker.clean();
            if (uri) {
              RNFS.unlink(uri);
            }
          }, 100000);
          clearTimeout(time);
        }
      })
      .catch((err) => {
        // console.log(err);
      });
  };

【问题讨论】:

  • 你完成installation step for android了吗?另外,尝试清理和重建调试 apk
  • 是的,我错过了指定的路径。 ... 路径:Platform.OS === 'android' ? file://${uri} : uri 而不是 path: uri。感谢您的评论

标签: android react-native react-native-android


【解决方案1】:

你需要添加android特定的路径值。

import { Platform } from 'react-native';

    ImageCropPicker.openCropper({
      path: Platform.OS === "android" ('file://' + uri) : uri,
      width: 300,
      height: 400,
      cropping: true,
      freeStyleCropEnabled: true,
    })

【讨论】:

  • @TanTaiNguyen 如果我的回答有用,请点击它左侧的点赞按钮 (▲)。如果它回答了您的问题,请单击复选标记 (✓) 接受它。这样其他人就知道你已经(充分地)得到了帮助。另请参阅当有人回答我的问题时我该怎么办?
  • 哦,太好了。非常感谢
  • @hongdeveloper openCropperopenPicker 提供的react-native-image-crop-picker 提供的两种方法有什么区别,我们可以使用它们中的任何一种来挑选、裁剪、设置图像吗?因为我使用的是openPicker&当我在Android上设置cropping:true时,它会崩溃而没有任何错误,&当cropping:false它工作正常。
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 2021-09-28
  • 1970-01-01
  • 2020-03-13
  • 2021-01-09
  • 2020-12-14
  • 1970-01-01
  • 2020-07-17
相关资源
最近更新 更多