Introduction
Ionic is a popular, open-source, and cross-platform mobile app framework that is helpful in developing or building hybrid mobile apps quickly and easily. It will save you a lot of time and money.
This article demonstrates how to take a picture in a camera and save the picture's specific path in Ionic 3 using a native camera plugin. The following tools and requirements should be prepared before starting this article.
Once we've installed Node.js, we are able to install other tools on the terminal or command line. Run the following command.
npm install -g ionic cordova
Once the installation is finished, let’s start creating an Ionic 3 app.
Create an Ionic 3 app
Open your node.js terminal and open a specific location to create the Ionic 3 app. Start a new Ionic project using the ionic start command.
ionic start myApp
This command will take a few minutes because it has installed all dependencies and modules in a project. Now, change the location to myApp.
cd myApp
Once we've changed the location of myApp, add an Android platform to your app.
ionic cordova platform add android
ionic cordova platform add browser(For Browser)
Now, run the project using the following command.
ionic serve (For Browser)
ionic cordova run android(For Device)
Make sure that you have connected the device and the settings have been set.
Now, let’s start.
- Camera
The Camera Plugin is used to take a picture and also capture a video.
- File
Using the File Plugin, we can achieve the following -
- Get the available free space
- Check if the directory is available or not
- Create a directory
- Remove a directory
- Move a directory from one place to another place,
- Copy a directory
- List an available directory
- Set pre-defined path
- Check if a file is available or not
- Create a file
- Remove a file
- Write a file
- Read a file as a text
- Read a file and return base64 data
- Read a file and return data in a binary format
- Read a file and return data in an array buffer format
- Move and copy files
- Get a directory or file etc.
Install and Configure Camera and File
Follow the below-mentioned steps.
Step1
Now, we have an Ionic app so we can move forward. First, install the camera plugin to our Ionic app using the following steps.
ionic cordova plugin add cordova-plugin-camera
npm install --save @ionic-native/camera
Now, install the file plugin to the Ionic app using the following steps.
ionic cordova plugin add cordova-plugin-file
npm install --save @ionic-native/file
Step 2
Now, we have already installed a camera plugin and file so we need to add a Camera plugin and File plugin to our app module file.
app.module.ts file
-
- import {
- Camera
- } from '@ionic-native/camera';
- import {
- File
- } from '@ionic-native/file';
- @NgModule({
- providers: [
-
- Camera,
- File
- ]
- })
- export class AppModule {}
Step 3
Now, it’s time to integrate with a component in our app.
app.ts file
- import {
- Camera,
- CameraOptions
- } from '@ionic-native/camera';
- import {
- File
- } from '@ionic-native/file';
-
- constructor(private camera: Camera, private file: File) {}
-
- public getPicture() {
- let base64ImageData;
- const options: CameraOptions = {
-
- quality: 100,
-
-
-
-
-
-
- destinationType: this.camera.DestinationType.DATA_URL,
-
-
-
-
-
- encodingType: this.camera.EncodingType.JPEG,
-
-
-
-
- mediaType: this.camera.MediaType.PICTURE,
-
-
-
-
-
-
- sourceType: this.camera.PictureSourceType.CAMERA
- }
- this.camera.getPicture(options).then((imageData) => {
-
- base64ImageData = 'data:image/jpeg;base64,' + imageData;
-
-
-
-
-
-
-
- this.writeFile(base64ImageData, “My Picture”, “sample.jpeg”);
- }, (error) => {
- console.log(Error Occured: ' + error);
- });
- }
-
- public writeFile(base64Data: any, folderName: string, fileName: any) {
- let contentType = this.getContentType(base64Data);
- let DataBlob = this.base64toBlob(content, contentType);
-
- let filePath = this.file.externalRootDirectory + folderName;
- this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {
- console.log("File Writed Successfully", success);
- }).catch((err) => {
- console.log("Error Occured While Writing File", err);
- })
- }
-
- public getContentType(base64Data: any) {
- let block = base64Data.split(";");
- let contentType = block[0].split(":")[1];
- return contentType;
- }
-
- public base64toBlob(b64Data, contentType) {
- contentType = contentType || '';
- sliceSize = 512;
- let byteCharacters = atob(b64Data);
- let byteArrays = [];
- for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
- let slice = byteCharacters.slice(offset, offset + sliceSize);
- let byteNumbers = new Array(slice.length);
- for (let i = 0; i < slice.length; i++) {
- byteNumbers[i] = slice.charCodeAt(i);
- }
- var byteArray = new Uint8Array(byteNumbers);
- byteArrays.push(byteArray);
- }
- let blob = new Blob(byteArrays, {
- type: contentType
- });
- return blob;
- }
Step 4
Calling the getPicture method.
For further details, visit the official website.
- Camera
https://ionicframework.com/docs/native/camera/
- File Writing
https://ionicframework.com/docs/native/file/
- File Location
Where to Store a file,
https://github.com/apache/cordova-plugin-file#where-to-store-files
In this article, we discussed how to take a picture in a camera and save a picture's specific path in Ionic 3 using a native camera plugin.
If you have any questions/issues about this article, please let me know in the comments.