I am a beginner in Ionic 2. I would like to use the camera in Ionic. I followed https://ionicframework.com/docs/native/camera/ tutorial.
I have already installed the cordova-plugin-camera
plugin and installed ionic-native/camera using cli code.
While I serve the project it shows Runtime Error:
Uncaught(in promise):Error: No provider for Camera! injectionError
I am sending the app.module.ts, html page, and type script page. Please have a look.
app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HttpModule } from '@angular/http';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CameraExamplePage } from "../pages/camara-example/camara-example";
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CameraExamplePage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CameraExamplePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
Camera Take Html Page
<ion-header>
<ion-navbar>
<ion-title>camaraExample</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button color="dark" (click)="takePhoto()" > Take Photo
</button>
<img [src]="imageURL" *ngIf="imageURL">
</ion-content>
**TypeScript File **
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera'
@IonicPage()
@Component({
selector: 'page-camara-example',
templateUrl: 'camara-example.html',
})
export class CameraExamplePage {
imageURL
constructor(public navCtrl: NavController, public navParams: NavParams, public camera: Camera) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad CameraExamplePage');
}
takePhoto()
{
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
//let base64Image = 'data:image/jpeg;base64,' + imageData;
this.imageURL = imageData
}, (err) => {
// Handle error
});
}
}
Angular runtime gives error-nullinjectorerror: no provider for httpclient! NullInjectorError: No provider for HttpClient! The issue is more due to not registering the required services i.e HttpClientModule in the root module ie.
Angular runtime gives error-nullinjectorerror: no provider for httpclient! NullInjectorError: No provider for HttpClient! The issue is more due to not registering the required services i.e HttpClientModule in the root module ie. NgModule.
CameraOptions is not a provider class.. you create a new object of it.. There's a problem with the auto generated import statement. You need: `import { Camera, CameraOptions } from '@ionic-native/camera/ngx'; simply set Camera as provider in app.module.ts also import it in your component...works like magic! elaborate your answer.
You need to set Camera as provider in app.module.ts
import { Camera } from '@ionic-native/camera';//import in app.module.ts
//...
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
Camera //here
]
While I Serve the project it shows Runtime Error
NOTE: Cordova plugins do not work in ionic serve.. You need to use emulator/device.
Also, include your plugin code within this.platform.ready()
and check if cordova is available using this.platform.is('cordova')
import { Platform } from 'ionic-angular'; //import Platform
//...
constructor(public platform:Platform){}
//...
takePhoto() {
this.platform.ready().then(() => {
if(this.platform.is('cordova')){
this.camera.getPicture(this.options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
let base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
// Handle error
});
}
})
}
you need to install camera plugins first by using two commands
$ ionic cordova plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera
after that in your app.module.ts you need to import
that plugins
and change your provider
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HttpModule } from '@angular/http';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CamaraExampalePage } from "../pages/camara-exampale/camara-exampale";
import { Camera} from '@ionic-native/camera';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CamaraExampalePage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CamaraExampalePage
],
providers: [
Camera,
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
There's a problem with the auto generated import
statement. You need: import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With