Here is an Example to Take Screenshot Programmatically in React Native. To take Screenshot of your screen we have an inbuilt Screenshot option provided by mobile manufacturers but you can also make this in your own Application too. To take Screenshot we will use a very good library called react-native-view-shot
. Which provides captureScreen API which is very easy to use.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install thereact-native-cli
command line utility. Open the terminal and go to the workspace and runInstallation of Dependency
To use captureScreen
we need to install react-native-view-shot
package. To install this
Open the terminal and jump into your project
1 | cd Projectname |
Run the following command
1 | npm install react-native-view-shot --save |
This command will copy all the dependencies into your node_module directory, You can find the directory in node_module
the directory named react-native-view-shot
.
–save is optional, it is just to update the react-native-view-shot dependancy in your package.json file.
Dependency Version for this Example used is
1 | "react-native-view-shot": "^2.4.0" |
Linking of Library
To install the react-native-view-shot
library completely we have to make some changes in Android and IOS project files to link the library properly. In order to link the library you have to run following command:
1 | react-native link react-native-view-shot |
You can see the result like this
You can see the following changes in your android project. If you are unable to find the changes then please do those changes as they are must for this example.
Go to the ScreenshotExample> android > app > build.gradle.
Scroll down and you can see the following dependency and if not please add this.
1 | compile project(':react-native-view-shot') |
Open ScreenshotExample> android > settings.gradle and you can see the following lines and if not please add this.
1 2 | include ':react-native-view-shot' project(':react-native-view-shot').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-view-shot/android') |
Open ScreenshotExample> android > app > src > main > java > com > screenshotexample> MainApplication.java and you can see the following lines in imports if not then please add
1 | import fr.greweb.reactnativeviewshot.RNViewShotPackage; |
Scroll down and you can see the following line in getPackages after new MainReactPackage() if not then please add a comma(,) and add
1 | new RNViewShotPackage() |
Here we have done with our installation of the library. If you are still facing the trouble please comment below so that we can help you out from the problem.
You can download the source code from Github
OR
Open App.js in any code editor and replace the code with the following code
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /*This is an React Native Example to Take Screenshot Programmatically */ import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, Button, Platform } from 'react-native'; import { captureScreen } from "react-native-view-shot"; export default class App extends Component { constructor(){ super(); this.state={ //initial image to the <Image> imageURI : 'http://aboutreact.com/wp-content/uploads/2018/07/sample_img.png' } } takeScreenShot=()=>{ //handler to take screnshot captureScreen({ //either png or jpg or webm (Android). Defaults to png format: "jpg", //quality 0.0 - 1.0 (default). (only available on lossy formats like jpg) quality: 0.8 }) .then( //callback function to get the result URL of the screnshot uri => this.setState({ imageURI : uri }), error => console.error("Oops, Something Went Wrong", error) ); } render() { return ( <View style={styles.MainContainer}> <Text style={{fontSize:20}}>Click on Button Below to Take ScreenShot</Text> <Image source={{uri : this.state.imageURI}} style={{width: 200, height: 300, resizeMode: 'contain', marginTop: 5}} /> <Button title="Take Screenshot" onPress={this.takeScreenShot} /> </View> ); } } const styles = StyleSheet.create({ MainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', borderWidth: 1, borderColor: '#000', } }); |
To Run the React Native App
Open the terminal again and jump into your project using.This is how you can take screenshot programmatically. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
How useful was this post?
Click on a star to rate us!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!