HTTP POST request failing specifically on Android

Please provide the following:

  1. SDK Version: 42
  2. Platforms(Android/iOS/web/all): Android

I am developing an iOS/Android app with React Native and I am getting an error on Android.

I am using the expo-image-picker library ImagePicker - Expo Documentation

This is the code which works perfectly on iOS and not on Android:

    const prepareResult = await ImagePicker.launchImageLibraryAsync({
      quality: 0.5,
      capture: true,
      aspect: [4, 3],
      allowsEditing: true,
      allowsMultipleSelection: false,
      mediaTypes: ImagePicker.MediaTypeOptions.Images,
    });

    const androidResult = await ImagePicker.getPendingResultAsync();
    const result = androidResult?.length ? androidResult[0] : prepareResult;

    if (!result.cancelled) {
      const localUri = result.uri;
      const filename = localUri.split("/").pop();
      const match = filename ? /\.(\w+)$/.exec(filename) : "";
      const type = match ? `image/${match[1]}` : `image`;
      const formData = new FormData();
      formData.append("file", { uri: localUri, name: filename, type });

      fetch(`${API}/uploadAvatar`, {
        method: "POST",
        headers: {
          Authorization: bearer,
          "X-Requested-With": "XMLHttpRequest",
          "Content-Type": "application/json",
        },
        body: formData,
      })
        .then((response) => response.json())
        .then((json) => {
          setImage(json.file);
        })
        .catch((err) => {
          console.log({ err });
          Alert.alert("There was an error uploading your profile picture.");
        });
    }

On Android after I crop the image, it goes straight to the .catch method without even going through any of the .then methods.

I checked this ImagePicker - Expo Documentation which contains a note:

Note: Make sure that you handle MainActivity destruction on Android. See ImagePicker.getPendingResultAsync.

Any ideas about this error?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.