不幸的是应用程序停止了 - cordova - 插件 - 相机

时间:2016-07-13 08:13:29

标签: android cordova camera cordova-plugins

我正试图在我的cordova应用程序中从相机捕获图像

 <div onclick="getImage();">
    <img width="60" height="60" id="img0" />
</div>

我的getImage函数

function getImage() 
{
    navigator.camera.getPicture(onCaptureTaskPhoto, onFail, {quality: 25,targetWidth: 512, destinationType: destinationType.FILE_URI, correctOrientation: true});
}

我的onCaptureTaskPhoto函数

function onCaptureTaskPhoto(fileURI) 
{     
   var image1=document.getElementById('img0');
   image1.src =fileURI;
   image1.width="80";
   image1.height="80";
}

现在有时我能够捕获图像并将其放入img的src中。有时它显示异常。应用程序将是unfortunately app has stopped

手机的内部存储空间为400MB。 这是我得到的例外

E/AndroidRuntime(13231): java.lang.RuntimeException: 
Unable to resume activity {com.app/com.app.MainActivity}:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,
request=34, result=-1, data=null} to activity
{com.app/com.app.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String android.net.Uri.toString()' on a null object reference

1 个答案:

答案 0 :(得分:0)

这是完整的过程如何在离子中使用相机...

首先将相机插件添加到您的项目中:

cordova plugin add org.apache.cordova.camera

services.js代码

    .factory('Camera', function($q) {

   return {
      getPicture: function(options) {
         var q = $q.defer();

         navigator.camera.getPicture(function(result) {
            q.resolve(result);
         }, function(err) {
            q.reject(err);
         }, options);

         return q.promise;
      }
   }

});

HTML代码

    <button class = "button" ng-click = "takePicture()">Take Picture</button>
    <button class = "button" ng-click = "getPicture()">Open Gallery</button>
    <img ng-src = "{{user.picture}}">

控制器代码

    .controller('MyCtrl', function($scope, Camera) {

   $scope.takePicture = function (options) {

      var options = {
         quality : 75,
         targetWidth: 200,
         targetHeight: 200,
         sourceType: 1
      };

      Camera.getPicture(options).then(function(imageData) {
         $scope.picture = imageData;;
      }, function(err) {
         console.log(err);
      });

   };

})