ngCordova camera functions properly on emulator, but encounters issues on actual devices

I am currently developing an app using Ionic for my Course Conclusion, and I am facing an issue with the ngCordova Camera Plugin. The deadline is approaching fast as I only have 2 weeks left to complete this project.

While testing the app on the emulator, the camera functions properly without any issues. However, when I tried running it on my actual device, no image appeared. Instead of showing a broken image icon, it just displayed a blank square.

On the emulator, I tested the app using 3 different Android API versions: 17, 19, and 22, and it worked fine on all three. But when I tried running it on my device with API 22, the camera feature failed to work.

I am using Genymotion emulator along with >ionic run android. Here are the details:

Emulator: Xperia Z - Android 4.2.2 API 17

However, when I build the app using >ionic build android and install it on my device, this is what happens:

Device: Xperia Z2 - Android 5.1 API 22

The issue persists for all images on every page, whether I capture a new photo or select one from the library. Below is the code snippet from where the pictures come from:

form.html (modal):

<!-- Code snippet removed for brevity -->

This is the controller for the page where the modal is launched (only the camera part has been shown here, additional code can be provided if necessary):

<!-- Code snippet removed for brevity -->

I have already attempted the following troubleshooting steps:

  • Reinstalling the plugin
  • Reinstalling the platform
  • Changing from DATA_URL to FILE_URI
  • Removing 'data:image/jpeg;base64,' from the < img > tag and using it in the function with imageData

If anyone could provide assistance, it would be greatly appreciated.

EDIT:

Upon using Chrome Inspect, the following error was displayed when attempting to load an image:

Refused to load the image because it violates the Content Security Policy directive: "default-src *". Please note that 'img-src' was not explicitly set, so 'default-src' is being used as a fallback.

I have now added img-src 'self' *; to the meta tag on my index file, which resulted in a new error message:

Refused to load the image because it violates the Content Security Policy directive: "img-src *".

Answer №1

Consider updating img-src 'self' *; to img-src 'self' data:; This adjustment will enable the loading of resources using the data scheme (such as Base64 encoded images).

Answer №2

1) I noticed a mistake in your code on this line

$scope.fotoperfil = imageData; The variable imageData contains only a base64 string, so it cannot be displayed in the UI.

It should instead be $scope.fotoperfil = "data:image/jpeg;base64," + imageData; here data:image/jpeg;base64 converts the base64 string into a .jpeg image.

2) Don't forget to add a placeholder in your img tag for previewing the taken picture.

3) For an easy way to implement camera functionality, check out Nic Raboy's blog post here.

Update your HTML img tag like this

<img ng-show="imgURI !== undefined" ng-src="{{imgURI}}">
<img ng-show="imgURI === undefined" ng-src="http://placehold.it/300x300">

and include

<script src="js/ng-cordova.min.js"></script>

in your html file.

Also, modify your module as follows

angular.module('starter', ['ionic', 'ngCordova'])

Depending on your requirements, use DATA_URL or FILE_URI as explained in the ng-Cordova camera documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Global scope controller constructors do not seem to be functioning

After watching a tutorial video, I tried running this basic AngularJS code locally but encountered a different output than expected. Instead of the message displaying properly, it shows {{message}}. <!DOCTYPE html> <html lang="en" ng-app> < ...

Exploring the topic of AngularJS unit testing and working with httpBackend timeouts

I have experience in the testing world, particularly with TDD using tools like mocha, sinon, chai, and nodejs. Recently, I've been finding AngularJS testing a bit challenging to grasp and implement. Currently, I am trying to test the simplest method ...

Is there a way to track when the Angular DTOptionsBuilder ajax call is complete and trigger a callback function?

Working with angular datatables, I have the following code: beforeSend:

success callback causes the table on the page not to populate with the data. How can I implement a callback that triggers once the ajax is done without interfering with the nor ...

Ensuring Form Accuracy - Mandatory Selection from Group

Currently, in the project I am working on, there are three textboxes that need to be validated to ensure at least one of them has been filled out. I have been researching custom validation with Angular directives and found that it is possible to set the v ...

The unique GopikaTwo Gujarati font is unfortunately not compatible with Android mobile browsers and hybrid applications

I am currently trying to incorporate the custom font GopikaTwo into my hybrid application. Below is the code snippet I have added to my CSS file: @font-face { font-family: 'GopikaTwo' !important; src: url('GopikaTwo.eot') !impo ...

adjusting the template of the @Component programmatically

The template I am using is generated dynamically based on the intricate rules defined in the Java layer. I am wondering if there is a method to dynamically assign or modify a component's template during ngOnInit or any other suitable point in the cod ...

Provide the aggregated content within d3's text() or html() function

Below is my d3 code snippet: grouping.append('foreignObject').html(function (d) { var string = '<p>hello, {{ "there" }} <some-directive></some-directive></p>'; string = $compile(string)(scope); return stri ...

Generating data for a table by choosing a row from another table through ng-repeat

One of the functionalities I am working on involves a table where users can select an option and have that selection populate another table called 'User Details'. The table is populated with data retrieved from an API call in the angular controll ...

Can someone explain the role of $ctrl in angularjs and when it is more appropriate to use $ctrl instead of $scope in

In this illustration, the variable $ctrl is utilized in the view <b>Heroes</b><br> <hero-detail ng-repeat="hero in $ctrl.list" hero="hero" on-delete="$ctrl.deleteHero(hero)" on-update="$ctrl. ...

What is the best way to transfer information from a model to the screen?

I need assistance with adding a Todo using a model. When I click the Add todo button located at the bottom of the page, it opens the model. I want to be able to add whatever I type in the input field to the list. How can I send the input field data to the ...

Is there a way to implement CSS stylesheets on a particular component?

I am currently working on a project that involves developing an Android app and a web app within the same Next.js project. I am trying to ensure that the recommended Ionic global stylesheets do not affect the web layout. Within my server component app/lay ...

What is the best way to retrieve additional data from a JSON file on an Android device?

Aspiring to develop an Android application for a specific website, I encountered a challenge. The site only displays 10 posts per page, with additional posts spread across multiple pages. My goal is to implement a feature where, upon scrolling down, the ap ...

AngularJS shows as "true" yet remains in a state of "ng-hide" due to its specified class

I am trying to toggle the visibility of a div using ng-show and ng-hide in AngularJS. The page is loaded using ng-include. <body ng-app="myApp" ng-controller="FirstCtrl"> <div ng-include="home.html"> </div> </body> Content ...

Integrating Spring MVC with AngularJS for File Upload results in an error message: org.apache.commons.fileupload.FileUpload

I have a back-end Java Spring MVC Web application with an AngularJS front-end. In my AngularJS application, I need to upload a file to the server. Below is the HTML code: <form ng-submit="uploadFile()" class="form-horizontal" enctype="multipart/form- ...

When switching tabs, Ion-select should not reload the selected name

Whenever I switch tabs and then return to the previous tab in Ionic, the select field that was previously set becomes null, even though the page is still loading and the variable is populated. <ion-header color="primary"> <ion-navbar> &l ...

AngularJS ng-repeat not displaying bold fonts in HTML output

Despite successfully outputting HTML for the most part, some of the styling seems to be getting stripped out. As you can see in the images below, the /ul/ is properly rendering the /li/ items within an ng-repeat, but the BOLD font is not displaying correct ...

Tips for running a post-installation script when deploying an Angular 4 application to an Azure website using KuduScript

I recently faced an issue with my angular4 app deployed on azure web app (website) using Deployment Options through the Portal. To customize the deployment script, I utilized kuduscript [kuduscript -y –node] and modified deploy.cmd to specify how Azure s ...

Issues Persist with Updating mySQL Database using PHP

I've been attempting to insert location data into a MySQL database using my PHP server from the TripTracker Android App. However, despite trying different approaches in PHP, I have not been successful so far. Any assistance would be much appreciated. ...

Ways to launch angularjs project in production mode sans the use of npm start

Currently, I am in the process of developing a website with angularjs and have set up a local development server using npm. After moving my project directory to a public AFS directory, I encountered an issue where the npm dependencies are not functioning ...

IE9 causing issues with Angularjs ng-route - views fail to display

I am new to AngularJS and currently working on developing an application using AngularJS along with Coldfusion for database data retrieval. However, I am facing compatibility issues specifically with IE9 (which is the default browser in my office). The ap ...