How to display local image in React Native Application ? - GeeksforGeeks (2024)

Last Updated : 25 Jul, 2024

Summarize

Comments

Improve

In this article, we will learn to display an image from a phone gallery in a React Native application. We will use react-native-image-picker to select the image and display it in our application. You can find the API reference for this package at the end of this article.

Step 1: To initialize a new React Native Application, execute the following command:

npx react-native init LocalImagePicker

Step 2: Now, move into the project folder and install the react-native-image-picker package, and to do so, execute the following command:

cd LocalImagePicker
npm i react-native-image-picker

Project structure: It should look like.

How to display local image in React Native Application ? - GeeksforGeeks (1)

Project Structure

We need to modify the App.js file, and develop the following components, for the application to pick and display the local image:

  1. Button: A basic React Native button component that opens up the gallery, when the user taps the button.
  2. Image: A basic React Native Image component for displaying the response image from the picker.

We need to import the launchImageLibrary from react-native-image-picker, which takes options and callback functions as arguments. Using options, you can specify the mediaType, selectionLimit, maxHeight, maxWidth, and other attributes. The callback method is called with the response object, which will basically set our pickerResponse state variable.

App.js
import React,{useState} from 'react';import type {Node} from 'react';import { Button, SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, View, Image} from 'react-native';import { Colors,} from 'react-native/Libraries/NewAppScreen';import { launchImageLibrary } from 'react-native-image-picker';const App: () => Node = () => { const isDarkMode = useColorScheme() === 'dark'; const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, flex:1, alignItems: 'center', justifyContent: 'center' }; const [pickerResponse, setPickerResponse] = useState(null); const openGallery = () => { const options = { selectionLimit: 1, mediaType: 'photo', includeBase64: false, }; launchImageLibrary(options, setPickerResponse); }; const uri = pickerResponse?.assets && pickerResponse.assets[0].uri; return ( <SafeAreaView style={backgroundStyle}> <Button title="Pick from Gallery" onPress={openGallery} /> { uri && ( <Image source={{uri}} style= {{height:400, width:400,margin:20}}> </Image> ) } </SafeAreaView> );};export default App;

We have created a state variable, pickerResponse, which holds the response object. We create the openGallery method, where we set to define the options object, and call the launchImageGallery method. You can tweak these options and observe the response for better understanding. After the image is picked, the pickerResponse object will hold this asset and we access the image using uri. To render the image, we use conditional rendering, meaning only if the uri has been set, the image component will render with the source value as the URI.

Step to run application:To run the application, start the server, execute the following commands:

npx react-native start
npx react-native run-android

Output:

Library API reference: https://www.npmjs.com/package/react-native-image-picker



Please Login to comment...

How to display local image in React Native Application ? - GeeksforGeeks (2024)
Top Articles
What is a CD-ROM? – TechTarget Definition
Get started with Azure Synapse Link for Azure SQL Database - Azure Synapse Analytics
Section 4Rs Dodger Stadium
Lifewitceee
Goodbye Horses: The Many Lives of Q Lazzarus
Practical Magic 123Movies
Asian Feels Login
Aiken County government, school officials promote penny tax in North Augusta
Xrarse
Buckaroo Blog
Mycarolinas Login
How Many Slices Are In A Large Pizza? | Number Of Pizzas To Order For Your Next Party
Bestellung Ahrefs
Illinois Gun Shows 2022
Finger Lakes Ny Craigslist
Virginia New Year's Millionaire Raffle 2022
Wausau Obits Legacy
Google Doodle Baseball 76
X-Chromosom: Aufbau und Funktion
Kaitlyn Katsaros Forum
Maxpreps Field Hockey
Busted Mcpherson Newspaper
Craigslist St. Cloud Minnesota
Egizi Funeral Home Turnersville Nj
The Many Faces of the Craigslist Killer
From This Corner - Chief Glen Brock: A Shawnee Thinker
27 Modern Dining Room Ideas You'll Want to Try ASAP
Greyson Alexander Thorn
Ripsi Terzian Instagram
Smayperu
Bt33Nhn
42 Manufacturing jobs in Grayling
Skyrim:Elder Knowledge - The Unofficial Elder Scrolls Pages (UESP)
Dynavax Technologies Corp (DVAX)
Worcester County Circuit Court
Setx Sports
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Parent Portal Pat Med
Rage Of Harrogath Bugged
Arnesons Webcam
Big Reactors Best Coolant
Elven Steel Ore Sun Haven
Sandra Sancc
How the Color Pink Influences Mood and Emotions: A Psychological Perspective
Market Place Tulsa Ok
Boyfriends Extra Chapter 6
Ronnie Mcnu*t Uncensored
Sleep Outfitters Springhurst
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Hy-Vee, Inc. hiring Market Grille Express Assistant Department Manager in New Hope, MN | LinkedIn
How To Find Reliable Health Information Online
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 5729

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.