Blog Blog Posts Business Management Process Analysis

How to Build a React Native Image Picker?

Before starting on your journey of building a React Native Image Picker, it is essential to ensure you have a solid foundation. Prerequisites include a basic understanding of React Native development, familiarity with JavaScript and React concepts, and a working development environment with installed Node.js and a code editor. Also, knowledge of handling permissions, accessing the device’s photo library, and installing necessary dependencies like react-native-image-picker will be advantageous. 

Table of Contents:

Watch this ReactJS full course video to learn more about ReactJS:

{
“@context”: “https://schema.org”,
“@type”: “VideoObject”,
“name”: “ReactJS Full Course | ReactJS Tutorial for Beginners | ReactJS Training | Intellipaat”,
“description”: “How to Build a React Native Image Picker?”,
“thumbnailUrl”: “https://img.youtube.com/vi/MwgWuzO7fHs/hqdefault.jpg”,
“uploadDate”: “2023-07-21T08:00:00+08:00”,
“publisher”: {
“@type”: “Organization”,
“name”: “Intellipaat Software Solutions Pvt Ltd”,
“logo”: {
“@type”: “ImageObject”,
“url”: “https://intellipaat.com/blog/wp-content/themes/intellipaat-blog-new/images/logo.png”,
“width”: 124,
“height”: 43
}
},
“embedUrl”: “https://www.youtube.com/embed/MwgWuzO7fHs”
}

What is React Native Image Picker?

React Native Image Picker is a robust tool that empowers developers to incorporate image selection functionality seamlessly into their React Native applications. It allows users to select images from their device’s photo library or capture new ones via the camera. 

Enroll now in the React JS course to learn its concepts from experts.

Benefits of Building a React Native Image Picker

Benefits of Building a React Native Image Picker

Let’s explore the top benefits of building a React Native Image Picker:

Check out our blog on ReactJS Tutorial to learn more about ReactJS.

Building a React Native Image Picker

Building a React Native Image Picker

Step 1: Choosing React Native Image Picker

There are two well-known libraries available for implementing the image picker component in React Native:

Which one to choose? 

While React Native Image Picker is a popular module for selecting media from the device library or camera, React Native Image Crop Picker offers an additional advantage. 

Step 2: Create a new React Native project using typescript.

npx react-native init ImagePickerDemo  --template react-native-template-typescript

Or, 

import ImagePicker from 'react-native-image-crop-picker';

Step 3: Add the build.gradle android configuration.

allprojects {
    repositories {
      mavenLocal()
      center()
      maven { url "$rootDir/../node_modules/react-native/android" }
      // ADD THIS
      maven { url 'https://maven.google.com' }
      // ADD THIS
      maven { url "https://www.jitpack.io" }
    }
}
Add useSupportLibrary (android/app/build.gradle)
android {
    ...
    defaultConfig {
        ...
        vectorDrawables.useSupportLibrary = true
        ...
    }
    ...
}
Use Android SDK >= 26 (android/app/build.gradle)
android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    ...
    defaultConfig {
      ...
      targetSdkVersion 27
      ...
    }
    ...
}

Step 4: Building the profile avatar

import React from 'react';
import {Image, ImageProps, StyleSheet, TouchableOpacity} from 'react-native';
import ImagePicker, {ImageOrVideo} from 'react-native-image-crop-picker';
interface AvatarProps extends ImageProps {
  onChange?: (image: ImageOrVideo) => void;
}
export const Avatar = (props: AvatarProps) => {
  const [uri, setUri] = React.useState(props.source?.uri || undefined);
  const pickPicture = () => {
    ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true,
    }).then(image => {
      setUri(image.path);
      props.onChange?.(image);
    });
  };
  return (
    
      
    
  );
};
const styles = StyleSheet.create({
  avatar: {
    paddingTop: 20,
    height: 100,
    width: 100,
    borderRadius: 100,
    padding: 20,
  },
});

Step 5: Select pictures and videos from the device’s gallery or camera

To use the camera, we only need to call the openCamera function instead of openPicker:

import React from 'react';
import {
  Image,
  ImageProps,
  Pressable,
  SafeAreaView,
  StyleSheet,
  Text,
  TouchableOpacity,
} from 'react-native';
import ImagePicker, {ImageOrVideo} from 'react-native-image-crop-picker';
import Modal from 'react-native-modal';
import {CameraIcon, ImageIcon} from './icons';
interface AvatarProps extends ImageProps {
  onChange?: (file: ImageOrVideo) => void;
}
export const Avatar = (props: AvatarProps) => {
  const [uri, setUri] = React.useState(props.source?.uri || undefined);
  const [visible, setVisible] = React.useState(false);
  const close = () => setVisible(false);
  const open = () => setVisible(true);
  const chooseImage = () => {
    ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true,
    })
      .then(image => {
        setUri(image.path);
        props.onChange?.(image);
      })
      .finally(close);
  };
  const openCamera = () => {
    ImagePicker.openCamera({
      width: 300,
      height: 400,
      cropping: true,
    })
      .then(image => {
        setUri(image.path);
        props.onChange?.(image);
      })
      .finally(close);
  };
  return (
    
      
        
      
      
        
          
            
            Library 
          
          
            
            Camera
          
        
      
    
  );
;
const styles = StyleSheet.create({
  avatar: {
    paddingTop: 20,
    height: 100,
    width: 100,
    borderRadius: 100,
    padding: 20,
  },
  options: {
    backgroundColor: 'white',
    flexDirection: 'row',
    borderTopRightRadius: 30,
    borderTopLeftRadius: 30,
  },
  option: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});
ImagePicker.openPicker({
  width: 300,
  height: 400,
  cropping: true
}).then(image => {
  console.log(image);
});
ImagePicker.openPicker({
  multiple: true
}).then(images => {
  console.log(images);
});
ImagePicker.openPicker({
  mediaType: "video",
}).then((video) => {
  console.log(video);
});
ImagePicker.openCamera({
  width: 300,
  height: 400,
  cropping: true,
}).then(image => {
  console.log(image);
});
ImagePicker.openCamera({
  mediaType: 'video',
}).then(image => {
  console.log(image);
});

Step 6: Final code

import * as React from 'react';
import {View} from 'react-native';
import {ImageOrVideo} from 'react-native-image-crop-picker';
import {Avatar} from './Avatar';
export const Profile = () => {
  const onAvatarChange = (image: ImageOrVideo) => {
    console.log(image);
    // upload image to server here 
  };
  return (
        
  );
};

What are the Applications of Image Picker?

Let’s explore the top applications of Image Picker and how it enhances user experiences in different contexts.

What are the Applications of Image Picker

Preparing for interviews? Check out this React Interview Questions list!

Conclusion

Building a React Native Image Picker can greatly enhance the functionality and user experience of your applications. By following the step-by-step process outlined in this tutorial, you can seamlessly integrate an image selection feature into your React Native projects. From handling permissions to accessing the device’s photo library and utilizing third-party libraries like react-native-image-picker, you now possess the knowledge and tools to create a robust and user-friendly image picker. 

Embrace the power of visual media in your apps and provide your users with a convenient and delightful image selection experience. Start building your own React Native Image Picker today and unlock new possibilities for your applications.

Still in doubt? Put your query on Intellipaat’s community page.

The post How to Build a React Native Image Picker? appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/how-to-build-a-react-native-image-picker/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×