0

I'm pretty new using React Native (Expo in this case) and Firebase database.

My problem is that when I upload an image in my app thanks to Image Picker, the link is a local link, so reading only with my device, and then deleted when I erase the cache

Here is my code :

useEffect(() => {
    (async () => {
        if (Platform.OS !== "web") {
            const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
            if (status !== "granted") {
                alert("Sorry, we need camera roll permissions to make this work!");
            }
        }
    })();
}, []);

const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
        allowsEditing: true,
        quality: 1,
    });
    
    if (!result.cancelled) {
        setImage(result.uri);
    }
};

// My current image is locate to : "file:///data/user/0/host.exp.exponent/cache/
// ExperienceData/ImagePicker/2abe4097-05ed-4d23-5648-f279d5a6f995.jpg"

// And what I want is to locate my image to : "https://someting..."

So I want to convert this image uri link in a url link, to be shared and never erased.

Anyone has an idea about how to proceed ?

Thanks a lot !

maje
  • 3
  • 5
  • Please add more details, code snippet and examples, it's not really clear yet :) – vinalti Jan 22 '22 at 09:10
  • @vinalti I just add my code :) – maje Jan 22 '22 at 09:24
  • Answered a similar question here: https://stackoverflow.com/a/70580835/3832047. You would need to upload your file as base64 to your server, or alternatively stream it - streaming is better but requires more code. – Phobos Jan 23 '22 at 19:26
  • @Phobos, i tried but doesn't work for me. Do you have a link or something to try streaming ? I don't know what to search to help me Thank you ! – maje Jan 24 '22 at 15:24
  • What issue are you running into with the base64 option? I can guarantee it will be easier to solve than setting up your server for file streaming. – Phobos Jan 24 '22 at 17:56
  • Honestly, I don't really understand how it works. Do you have a contact where I can join you if you have the time :) – maje Jan 25 '22 at 15:02

1 Answers1

0

Let's break down the problem as below :

1. Pick an image from the Media Library.

const pickImage = async () => {
   let result = await ImagePicker.launchImageLibraryAsync({
       mediaTypes: ImagePicker.MediaTypeOptions.All,
       allowsEditing: true,
       quality: 1,
   });
   
   if (!result.cancelled) {
       setImage(result.uri);
   }
};


2. Fetch Image BLOB Data


  const blob = await new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function () {
      resolve(xhr.response);
    };
    xhr.onerror = function (e) {
      reject(new TypeError("Network request failed"));
    };
    xhr.responseType = "blob";
    xhr.open("GET", [IMAGE_URI_HERE], true);
    xhr.send(null);
  });

3. Upload image BLOB to a remote server (Firebase Storage)


  const metadata = { contentType: "image/jpg" };

  const imgRef = firebase.storage().ref('folderName/fileName');

  await imgRef.put(blob, metadata);

  // We're done with the blob, close and release it
  blob.close();
 
  // Image permanent URL 
  const imgURL = await imgRef.getDownloadURL();



Fiston Emmanuel
  • 4,242
  • 1
  • 8
  • 14
  • Really thanks ! I'm trying with that. The problem is that my import storage from '@react-native-firebase/storage' get an error : Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app(). I don't understand what that mean – maje Jan 22 '22 at 11:34
  • You need to install Firebase Storage - https://firebase.google.com/docs/storage/web/start – Fiston Emmanuel Jan 22 '22 at 13:51
  • Thanks for the help! I'm still on it, but the img.put() doesn't work. "It's not a function", and I don't understand why – maje Jan 24 '22 at 12:20
  • It seems that there's something wrong with Firebase setup. You can reach me at emmbyiringiro@gmail.com and I can look on your implementation in-depht – Fiston Emmanuel Jan 25 '22 at 10:14