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
Crypto Seed Phrases vs. Private Keys: What's the Difference?
12 reasons your debit card was declined + fixes | finder.com
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
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.