react-native-rich-editor icon indicating copy to clipboard operation
react-native-rich-editor copied to clipboard

Cannot display local image resources

Open qzlu-cyber opened this issue 3 years ago • 5 comments

It doesn't show up when I've selected an image. it prompts me 'Not allowed to load local resource: file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/xxxxxx/data/Containers/Data/Application/xxxxx/tmp/xxx.jpg'. This is my code:

  const selectImage = async () => {
    try {
      const result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        allowsEditing: true,
      });
      if (!result.cancelled) setImageUri(result.base64);
    } catch (error) {
      console.log("Error reading an image", error);
    }
  };

  let onPressAddImage = useCallback(() => {
    selectImage();
    // insert URL
    richText.current?.insertImage(imageUri);
    // insert base64
    // this.richText.current?.insertImage(`data:${image.mime};base64,${image.data}`);
  }, []);

When I click to add an image, onPressAddImage is called, but the image is not displayed. I have the same problem with this #217 But according to his method I didn't solve the problem. Could you please help me? @rafaelboniolo

qzlu-cyber avatar Feb 14 '22 09:02 qzlu-cyber

I created a correction PR but it still hasn't been accepted, it seems to me the error still persists in production!

rafaelboniolo avatar Feb 14 '22 23:02 rafaelboniolo

Sorry, in production my PR works correctly

rafaelboniolo avatar Feb 15 '22 11:02 rafaelboniolo

I solved this problem temporarily after using base64. Thank you very much

qzlu-cyber avatar Feb 16 '22 08:02 qzlu-cyber

I solved it like

import ImgToBase64 from 'react-native-image-base64';
import ImagePicker from "react-native-image-crop-picker";

 openGalleryClickProfile() {
    ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true,
    }).then((image) => {
      console.log("Imagemime", image); 
     this.onPressAddImage(image)
    });
  }

  onPressAddImage(image){
    ImgToBase64.getBase64String(image.path)
    .then((base64String) => {
      const str = `data:${image.mime};base64,${base64String}`
      this.richText.current?.insertImage(
        str
      );
    })
    .catch((err) => {
      console.log("base64:Image:", err)
    })};

DevVarsha avatar Jul 04 '22 12:07 DevVarsha