Ultimate Uganda Safari And Other Things To Do In Uganda

There are a ton of things for anyone to do on a tour visit to Uganda although it is still the least popular of all the East African nations for a safari experience. There are many spectacular…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Camera and Photo Gallery in Ionic React app using Capacitor

Camera and Photo Gallery in Ionic React app using Capacitor

If you are looking for Camera functionality in

Let’s see a brief intro to each of the included frameworks

You probably already know about Ionic, but I’m putting it here just for the sake of beginners. Ionic is a complete open-source SDK for hybrid mobile app development created by Max Lynch, Ben Sperry and Adam Bradley of Drifty Co. in 2013.

It is important to note the contribution of Cordova in this. Ionic is only a UI wrapper made up of HTML, CSS and JS. So, by default, Ionic cannot run as an app in an iOS or Android device. Cordova is the build environment that containerizes (sort of) this Ionic web app and converts it into a device installable app, along with providing this app access to native APIs like Camera etc.

So, in other words — If you create Native apps in Android, you code in Java. If you create Native apps in iOS, you code in Obj-C or Swift. Both of these are powerful but complex languages. With Cordova (and Ionic) you can write a single piece of code for your app that can run on both iOS and Android (and windows!), that too with the simplicity of HTML, CSS, and JS.

Now you have some idea of Cordova — Cordova helps build Ionic web app into a device installable app. But there are some limitations of Cordova, which Capacitor tries to overcome with a new App workflow.

Capacitor is a cross-platform app runtime that makes it easy to build web apps that run natively on iOS, Android, Electron, and the web. Ionic people call these apps “Native Progressive Web Apps” and they represent the next evolution beyond Hybrid apps.

Let’s check the differences between Cordova and Capacitor

In essence, Capacitor is like a fresh, more flexible version of Corodva.

Other than that, Capacitor also doesn’t support plugin installation with variables. Those changes have to be done manually in the native code.

(Read carefully)

Since Ionic 4, Ionic has become framework agnostic. Now you can create Ionic apps in Angular, React, Vue or even in plain JS. This gives Ionic great flexibility to be used by all kinds of developers.

It is important to note that Ionic React apps are only supported by Capacitor build environment.

Same is not true for Ionic Angular apps — Ionic Angular apps are supported by both Cordova and Capacitor build environments.

Hence, if you want to build apps in Ionic React, you need to use Capacitor to build the app on device.

But …

… To build camera / Photo gallery functionality, you can either opt to

I know if can get confusing as 4 frameworks are crossing their paths here. Bottom line for this post — Ionic + React + Capacitor + Camera (using Capacitor core functionality)

I will go step-by-step so everyone can benefit

Let’s get started with Ionic React Camera app !

First you need to make sure you have the latest Ionic CLI. This will ensure you are using everything latest (Duh ! ) . Ensure latest Ionic CLI installation using

Creating a basic Ionic-React app is not much different or difficult from creating a basic Ionic-Angular app. Start a basic blank starter using

You see what just happened. The --type=react told the CLI to create a React app, not an Angular app !!

Run the app in browser using (yes you guessed it right)

You won’t see much in the homepage created in the starter. Let’s modify this page to include a floating button and a placeholder for our selected image

The code for this layout isn’t very complex

Capacitor can be attached to an existing Ionic app as well. To attach Capacitor to your existing Ionic app, run

This will attach Capacitor to your Ionic app. After this, you have to init the Capacitor app with

It will ask you the app ID, which is the domain identifier of your app (ex: com.example.app)

Before building the app for Android, let’s first add the code for Camera functionality

Similar to a Cordova plugin, Capacitor camera functionality also allows both Camera and Gallery functionalities. Once you implement the Camera function using Capacitor, it will automatically ask you for Camera / Gallery option.

Import Camera in your app/page with this

There are few plugins in-built in Capacitor/core . You can access these by simply importing.

Implement camera capture or gallery selection method like this

Then you can call takePicture function from the floating button click using

That’s it ! Camera integrated ! Lol, don’t worry we’ll test it as well.

Before adding a platform to Capacitor, you need to build the app once. Build the app using

Now add Android platform (or iOS)

This should add Android platform to your project.

If you face an error saying

Check your capacitor.config.json file. Make sure the webDir field has a value of www . If not, change the webDir field to have the same value as the last parameter of the url showing up in the above error

Note, you cannot run the project directly on the phone using CLI in Capacitor. You will have to open the project in Android Studio and run using the Android Studio IDE. Open the project in Android Studio using

Now run the app in your device using Android Studio, and you get this ! (Sample from my One Plus 6T, Android P)

Capture image from Camera in Ionic React App using Capacitor
Capture image from Camera in Ionic React App using Capacitor

Selecting image from Gallery can be done with same code

Select image from gallery in Ionic React App using Capacitor
Select image from gallery in Ionic React App using Capacitor

🎉🎉🎉 Congrats ! You just implemented Camera and Gallery functionality using Capacitor, in Ionic React App !

As per the development philosophy of Capacitor goes, it is focused entirely on enabling modern web apps to run on all major platforms with ease. That also includes web-app. So let’s see whether our camera app can run on a web-browser as well

Let’s run the app in browser using ionic serve . You’ll probably see an error like this

Now import the package in your camera page (or app), and initialize after app is bootstrapped

and I’ll initialize it in the constructor

Now run the app in browser using ionic serve and you’ll be able to capture images using your web-cam (Chrome browser)

In the above example, we used minimum Camera options and got away with it. There are many more Camera options you can use to customize user’s experience of clicking/selecting images. Here’s the list of all the options

In this post, you learnt how to implement Camera and Photo Gallery functionality in your Ionic React apps using Capacitor. We also tested the functionality on Android and web-browser.

Now that you have learned the implementation of In-app purchase in Ionic React app, you can also try following blogs for Ionic Angular apps

Ionic 4 Full App with huge number of layouts and features
Ionic 4 Full App with huge number of layouts and features
Ionic 4 React Full App with huge number of layouts and features
Ionic 4 React Full App with huge number of layouts and features

Add a comment

Related posts:

Do You Surf Everyday? Get Paid To Surf!

Look at the image to the left. This just so happens to be an image that I keep in my head while I am surfing the web. Did you just ask WHY? Let me tell you, It reminds me that while I am surfing I…

The problem with black people

One nagging question that many people will have when observing the terrible racial injustices that go on the US and other parts of the world against black people is obviously: why do black people go…

Chingu Content Creators Guide

The Impostor Syndrome is something virtually all Developers struggle with. At its core, diagram (see Developers: How to Overcome Impostor Syndrome) below sums up this syndrome very accurately and…