Short-Video App with React Native and Supabase · Course book
Chapter 9: The Gallery and the Preview
Not every video is recorded inside the app. Many people film with the phone's own camera and decide later what to share, so a short-video app must also let users pick a clip from their gallery, check it in a preview and then upload it. This chapter adds that path. It explains how Expo's image picker selects videos, how photo library permissions differ from camera permissions, how to preview a clip with a video player before uploading, how to fit videos of different shapes onto a phone screen, worked out by hand, and how to cope with large files and different formats so uploads from the gallery are as reliable as those from the camera.
Choosing a video from the gallery
Expo's image picker opens the phone's own media browser, which users already know, and returns what they choose. Despite its name it handles videos too: setting the media types to videos only hides photos, and options control whether the user may trim the clip, the quality of the result and the maximum duration.
import * as ImagePicker from "expo-image-picker";
async function pickVideo() {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ["videos"],
allowsEditing: true,
quality: 1,
videoMaxDuration: 60,
});
if (result.canceled) return null;
const asset = result.assets[0];
return { uri: asset.uri, duration: asset.duration, width: asset.width, height: asset.height };
}
The result says whether the user cancelled, and if not, lists the chosen assets, each with a local uri, its dimensions, its duration and, depending on the platform, its file size and type. Option names have changed between versions of the library, for example in how media types are specified, so check the documentation for the version your project uses. Returning a small object with just the fields the app needs, as above, keeps the rest of the code independent of those details.
A gallery button usually sits beside the record button on the camera screen, showing a thumbnail of the most recent item as a hint. Tapping it opens the picker; choosing a clip moves to a preview screen rather than uploading straight away, because people often pick the wrong clip, and an accidental post is far more annoying than one extra tap.
Photo library permissions
Reading the gallery needs its own permission, separate from the camera. On recent versions of iOS, users can grant limited access, allowing the app to see only the photos and videos they select, rather than the whole library. The picker works well with limited access, because the system shows the user's own chooser and the app only receives what was picked. In many cases, launching the system picker does not need full library permission at all, which is the most privacy-friendly approach.
Android handles media access differently across versions. Newer releases offer a system photo picker that, like the iOS chooser, gives the app only the items the user selects without a broad storage permission, while older releases ask for permission to read media files. Expo's picker uses the appropriate mechanism for each version, but testing on both an older and a newer Android phone, as well as on iOS, is the only way to be sure the flow feels right everywhere.
As with the camera, the app must explain why it wants access, through a usage description in app.json, and must handle refusal politely, explaining what the user is missing and offering a link to the phone's settings. Asking only when the user taps the gallery button, rather than at launch, keeps the request understandable.
Previewing before upload
A preview screen shows the chosen clip playing in a loop, with buttons to go back and choose another, or to post it, and a text input for a title. The preview uses a video player component from Expo's audio and video library, given the local uri as its source.
Key player settings shape the preview. shouldPlay starts playback automatically; isLooping repeats the clip, as short-video apps do; isMuted can start the preview silent, with a button to turn sound on; and resizeMode decides how the video fits its frame. The player also reports its status, such as whether it is loading, playing or has finished, which can drive a progress bar or a loading spinner.
Uploading from the preview reuses the function written for the camera: read the file, upload it to the user's folder in the videos bucket, then insert the Video row with the title. Sharing one upload function between both paths means one place to fix bugs and add features such as progress reporting.
The preview screen is also the natural place for a few finishing steps. A thumbnail, a still image taken from the clip, makes profile grids and activity lists load quickly, because showing an image is far cheaper than loading a video; Expo offers a library that extracts a frame at a chosen time, and the thumbnail can be uploaded alongside the video and its path stored in the same row. The title input needs care on a phone: wrapping the bottom of the screen in a keyboard-avoiding view stops the keyboard covering the input, and tapping outside it should dismiss the keyboard. A character limit, shown as a small counter such as "42 / 150", keeps titles short enough to display over the video.
Finally, players hold memory and decoding resources while they exist. When the user leaves the preview, whether by posting or going back, the player should be stopped and unmounted, so that it does not keep playing sound in the background or slow down the feed that appears next. Navigating away with router.replace after a successful post achieves this naturally, because the preview screen is removed from the stack.
Fitting videos of any shape
Recorded clips from the app are portrait, but gallery videos can be any shape: landscape, square or portrait. The player's resize mode decides what happens when the video's shape differs from the screen's. Contain scales the video until it fits entirely inside the frame, leaving bars at the sides or top and bottom. Cover scales it until it fills the frame completely, cropping whatever overflows.
A sensible rule follows: use cover for portrait videos, which lose little, and contain for landscape ones, perhaps on a blurred copy of the video as the background, so nothing important is cut off. The picker returns each asset's width and height, so the app can choose the mode per video and even store the aspect ratio in the Video row for the feed to use.
Large files and formats
Gallery videos can be much larger than in-app recordings: a minute of 4K video can be hundreds of megabytes. Several safeguards help. The picker's maximum duration and quality options can reduce what is returned; the app can check the file size and refuse, or offer to compress, anything above a limit; and the upload should show progress and survive brief network drops, using resumable uploads for big files.
| Format or codec | Where it comes from | Notes for upload and playback |
|---|---|---|
| MP4 container with H.264 video | Most Android phones and many apps | Plays almost everywhere; the safest choice |
| MOV container (QuickTime) | iPhone camera | Plays on Apple devices; some browsers and Android players struggle |
| HEVC (H.265) video | Recent iPhones and some Android phones | About half the size of H.264 at similar quality, but less universally supported |
Setting the content type correctly when uploading, such as video/mp4 or video/quicktime, helps players handle each file. For the widest compatibility, larger apps re-encode every upload on a server into a standard format and several qualities, but for a project of this size, preferring compatible formats on the phone and testing playback on both platforms is enough, and keeps the project simple while it is still small.
Practice tasks
- A square 1080 × 1080 video is shown on a 390 × 844 screen. Compute the cover and contain scale factors and the displayed sizes.
- What share of a 4:3 landscape video's width is visible with cover on the same screen?
- Write the condition that chooses cover for portrait videos and contain for landscape ones, using the asset's width and height.
- Explain the difference between full photo library access and limited access, and why the system picker works with both.
- List three player settings used on the preview screen and what each does.
- Why should the camera and gallery paths share one upload function?
This course book is available to read online only at tech-academy.chrysolites.org.