I have seen that this issue has been presented a few times before however, those solutions haven't helped in completed this last task for my app.
I am using React Native with Expo. I am using Image Picker to select an image from iOS, and getting the uri suck as 'file:///var/mobile/Containers/Data/Application/9A9F8016-CB14-4772-BF42-91DC74EA40CC/Library/Caches/ExponentExperienceData/%2540anonymous%252Fheritagefilmsapp-5de39698-e6f1-4070-83cc-562fa650fca5/ImagePicker/855BDF4B-97F3-413C-BADA-F278CE645FAE.png'
I think I need to convert the uri image into binary data but for the life of me don't understand how to do that.
This is my useEffect hook to upload the image:
useEffect(() => {
if (!image) {
return;
}
function http_header_safe_json(v) {
return JSON.stringify(v);
}
var myHeaders = new Headers();
myHeaders.append(
"Authorization",
"Bearer TOKEN"
);
myHeaders.append(
"Dropbox-API-Arg",
http_header_safe_json({
path: `/${user.username}/!UserUploaded_1234.${fileType}`,
mode: {
".tag": "add",
},
autorename: true,
})
);
myHeaders.append("Content-Type", "application/octet-stream");
var file = imageData;
var requestOptions = {
method: "POST",
headers: myHeaders,
body: file,
redirect: "follow",
};
fetch("https://content.dropboxapi.com/2/files/upload", requestOptions)
.then((response) => response.json())
.then((result) => {
console.log(result);
})
.catch((error) => console.log("error", error));
}, [imageData]);
Here is my function to grab the uri:
const chooseFromLibrary = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
base64: true,
});
if (!result.cancelled) {
setImage(result.uri);
let cut = result.uri.split(".");
setImageData(result.uri);
setFileType(cut.pop());
}
}
Any help on this would be greatly appreciated.