Reputation: 497
I'm using expo FileSystem to get photos and videos from cameraRoll. is there any way to get these files extensions?
Upvotes: 3
Views: 7499
Reputation: 28539
If you have asked for permission and are able to get a response from the ImagePicker you should get an object that looks like this.
{
"cancelled": false,
"height": 2436,
"type": "image",
"uri": "file:///lots/of/directories/where/the/image/is/stored/ImagePicker/B69EA641-4738-4425-8319-FE190FC4BD6D.png",
"width": 1125,
}
If you call the ImagePicker like this (remember this is an await
so it should be wrapped in a try/catch
)
let result = await ImagePicker.launchImageLibraryAsync({mediaTypes:'Images'});
You can then access the uri by using result.uri
So we can do something like this
let uri = result.uri;
let fileExtension = uri.substr(uri.lastIndexOf('.') + 1);
Remember the user can cancel the request so you should check the result.cancelled
is false before trying to get the file extension, otherwise you will have issues as the uri will not exist.
The responses from using the Camera Roll aren't consistent across platforms. The following code will access the first video on the device.
let params = { first: 1, assetType: CameraRoll.AssetTypeOptions.Videos };
let result = await CameraRoll.getPhotos(params)
let videos = result.edges // the edges key stores an array of objects
{
node: {
group_name: 0,
image: {
height: 1280,
playableDuration: 56,
uri: "content://media/external/video/media/125",
width: 720,
},
timestamp: 1541516873,
type: "video/mp4",
},
}
{
node: {
group_name: "Camera Roll",
image: {
filename: "IMG_5030.MOV",
height: 1080,
isStored: true,
playableDuration: 612,
uri: "assets-library://asset/asset.MOV?id=10711D3D-EBDC-4EF2-A849-411D593A0B15&ext=MOV",
width: 1920,
},
location: {
altitude: 0,
heading: -1,
latitude: 0.0,
longitude: 0.0,
speed: -1,
},
timestamp: 1544786625,
type: "ALAssetTypeVideo",
},
}
As you can see Android
doesn't give a specific filename
, unlike iOS
which does. For iOS
you can extract the file extension using the method I described above. Unfortunately for Android
the only way to get the actual file extension is to use the ImagePicker
.
Upvotes: 5