react-native-cameraroll: iOS - returned uri cannot be used for upload image to server
Bug details
I am using the returned uri
from CameraRoll as a path to upload image to server, but the image cannot be upload. Below is an example of image uri
ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001
To Reproduce
RN Fetch Blob is used for making upload request.
RNFetchBlob.fetch("PUT", url, { <== url: is the path to our server
'Content-Type': contentType,
}, RNFetchBlob.wrap(path)) <== path: is the uri from CameraRoll: "ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001"
.uploadProgress((written, total) => {
progress(written, total);
})
.then((res) => {
if (R.path(["respInfo", "status"], res) === StatusCode.HTTP_200) {
success(res);
} else {
fail(res);
}
})
.catch((error) => {
fail(error);
})
This code is runnable with the Default CameraRoll in React native 0.57.4. After the library is moved to GitHub and change to use PHAsset, this code run fail. There is only one different thing that is the uri
return from CameraRoll
Expected Behavior
I am able to upload the image to server using the link ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001
Question:
From what I know, the link ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001
is from PHAsset of iOS. I wonder if it is really available to be used to upload image to server.
I the link is original unavailable for uploading image to server, then please let me know what should I do to convert that link.
Environment:
React Native Environment Info: System: OS: macOS High Sierra 10.13.6 CPU: x64 Intel® Core™ i5-2415M CPU @ 2.30GHz Memory: 36.78 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 10.13.0 - /usr/local/bin/node npm: 6.9.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1 Android SDK: Build Tools: 23.0.1, 23.0.2, 25.0.0, 25.0.2, 26.0.2, 26.0.3, 27.0.3, 28.0.0, 28.0.1, 28.0.2, 28.0.3 API Levels: 19, 21, 22, 23, 24, 25, 26, 27, 28 IDEs: Android Studio: 3.2 AI-181.5540.7.32.5056338 Xcode: 10.1/10B61 - /usr/bin/xcodebuild npmPackages: react: 16.6.0-alpha.8af6728 => 16.6.0-alpha.8af6728 react-native: 0.57.4 => 0.57.4 npmGlobalPackages: react-native-cli: 2.0.1
Thank you guys for helping me!
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 20
- Comments: 41 (4 by maintainers)
There seem to be good workarounds for this issue. I’m closing this, for now, just let me know if anyone wants to provide a PR that makes the above easier for everyone and I’ll re-open.
Hi to all, After analysing the iOS code I’ve found that it is fetching the asset for upload, but only if the uri starts with ph-upload, for images and also for videos. So, before upload
if (asset.uri.startsWith('ph://')) { asset.uri = 'ph-upload' + asset.uri.substring(2); }
should upload the original asset, not the thumbnail. You don’t need KA-32 solution, nor react-native-convert-ph-asset package.I have converted to
assets-library
, it is worked for me. this is exampleThis is still kinda difficult on iOS. Getting a photo or video from the user’s library so that you can upload it to a server is a common use case.
How can we make this easier?
I have the same issue, however, the suggested solution to convert PHAsset to assets-library is not stable.
Any other solution to this problem?
One workaround is to use
@react-native-community/image-editor
will return image path from:
to:
Here is a litte code turn video uri to old format.
For my part, I solved my problem. This was that the uri was not valid when converting images to base64. I solved it as follows:
First convert the
ph://
uri to an asset uri:Then copy the asset file to a temp location to use it, react-native-fs has very useful helpers for this.
I get error Error: ENOENT: no such file or directory, open ‘ph-upload://5A73648C-7869-4C77-B3C3-DF437B4F87BA/L0/001’ when use RNFS.readFile(imagePATH, ‘base64’)
@JoshuaGross passed the uri to RNFetchBlob and it fails. the error: “No such file ‘ph://AF32193E-88E0-44BD-BC1C-EA9920940D61/L0/001’”
I have solved this using the expo-media-library https://docs.expo.io/versions/latest/sdk/media-library/###
async myFunc() { let uri = “ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001” let myAssetId = uri.slice(5); let returnedAssetInfo = await MediaLibrary.getAssetInfoAsync(myAssetId); console.log(returnedAssetInfo.localUri); // you local uri link to get the file }
CameraRoll.saveToCameraRoll(photo.uri, 'photo').then( u => { let name = Date.now(); let data = new FormData() data.append ('file',{ uri : u, type: 'image/jpg', name: name+'.jpg' }) console.log('image upload',data) promises.push(axios.post(url,data)); } )
Need help with this was working fine. it only works with android. As said above i am getting path as below. ph://726E6F49-122A-44E8-84C4-3669CD8FFD59/L0/001
Thanks
https://github.com/jzyds/react-native-convert-ph-asset
I just submitted #76 which addresses the problem of uploading images as JPEG instead of HEIC. Might be related, I am not sure…
Hi, The workaround was just for uploading the image with fetch, not for using it with RNFS or other library.
This worked for me (credit to @gameboyVito):
to output:
"assets-library://asset/asset.MP4?id=60F75F00-0307-645E-875D-B0927CE2BB7D&ext=MP4".
Hi Guys, I found way to solve this issue in iOS. We can write another method like getSelectedPhoto() and pass the selected image path which is ph://ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED/L0/001. In RNCCameraRollManager.m we can use requestContentEditingInputWithOptions to get the path of selected image. I will raise PR on this
Is this has a fix or still unresolved ? i’m having the same issue