This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Take a screenshot
Edit page
In this tutorial, learn how to capture a screenshot using a third-party library, save it with Expo Media Library on Android and iOS, and download it on the web.
In this chapter, we'll learn how to take a screenshot using a third-party library and save it on the device's media library. We'll use react-native-view-shot to take a screenshot and expo-media-library to save an image on device's media library. Web browsers don't give apps access to a media library, so on the web, we'll download the screenshot as an image file.
So far, we have used third-party libraries, such as
react-native-gesture-handler,react-native-reanimated. We can find hundreds of other third-party libraries on React Native Directory depending on a use case.

Capture a screenshot with react-native-view-shot and save it to the device's media library using expo-media-library.
1
2
Prompt for permissions
An app that requires sensitive information, such as accessing a device's media library, has to prompt permission to allow or deny access. The usePermissions() hook from expo-media-library returns the permission permissionResponse and the requestPermission() method to ask for access. Our app only saves images to the media library, so we'll pass the writeOnly: true option to ask for write access without read access.
When the app loads for the first time and the permission status is neither granted nor denied, the value of the permissionResponse is null. When asked for permission, a user can either grant the permission or deny it. We can add a condition to check if it is not granted. If it is not granted, trigger the requestPermission() method. After getting the access, the value of permissionResponse.granted changes to true.
Add the following code snippet inside the src/app/(tabs)/index.tsx:
3
Create a ref to save the current view
We'll use react-native-view-shot to allow the user to take a screenshot within the app. This library captures the screenshot of a <View> as an image using the captureRef() method. It returns the URI of the captured screenshot image file.
In the previous chapter, we wrapped the <ImageViewer> and <EmojiSticker> components inside a <View>. We'll capture this <View>:
- Import
captureReffromreact-native-view-shot, and importuseRefand theComponentReftype from React. - Create an
imageRefreference variable to store the reference of the<View>. - Pass the reference variable to the
refprop of the<View>that wraps the<ImageViewer>and<EmojiSticker>components.
In the above snippet, the collapsable prop is set to false. This allows the <View> component to screenshot only of the background image and emoji sticker.
4
Capture a screenshot and save it
We can capture a screenshot of the view by calling the captureRef() method from react-native-view-shot inside the onSaveImageAsync() function. It accepts an optional argument where we can pass the width and height of the screenshot capturing area. We can read more about available options in the library's documentation.
The captureRef() method also returns a promise that fulfills with the screenshot's URI. We will pass this URI as a parameter to MediaLibrary.Asset.create(), which saves the screenshot to the device's media library.
Inside src/app/(tabs)/index.tsx, update the onSaveImageAsync() function with the following code:
Now, choose a photo and add a sticker in the app. Then tap the Save button. We should see the following result on Android and iOS:
5
Save the screenshot on the web
react-native-view-shot can also capture a screenshot on the web. However, expo-media-library works only on Android and iOS. On the web, we'll download the screenshot as an image file instead.
Using Platform module from React Native, we can implement platform-specific behavior. On the web, captureRef() returns the screenshot as a data URL when we set the result option to 'data-uri'. We can pass this data URL to a link element to download the image.
Inside src/app/(tabs)/index.tsx, update the onSaveImageAsync() function to check whether the current platform is 'web' with the Platform.OS property:
- If it is not
'web', we'll keep using the same logic added for Android and iOS. - If it is
'web', we'll use thecaptureRef()method to capture the current<View>as a JPEG image. Then, we'll create a link element with the data URL and click it to download the image.
The Platform module is already imported from react-native, because the imageContainer style uses it.
On running the app in a web browser, we can now save a screenshot:
Summary
Chapter 7: Take a screenshot
We've successfully used react-native-view-shot and expo-media-library to capture a screenshot and save it on the device's library, and to download it on the web.
The app does everything we set out for it to do. In the next chapter, we will customize the app's status bar, splash screen, and app icon.