简体   繁体   English

Ionic / Cordova:从手机图库上传图片

[英]Ionic / Cordova : Upload image from phone's gallery

I would like the user to be able to either take a picture or select an image from his phone's gallery. 我希望用户能够拍照或从手机的图库中选择图片。

I successfully manage to take a picture and get the imageURI . 我成功地拍摄了一张照片并获得了imageURI

I'm using Genymotion as emulator since i need to access to some functionalities such as camera. 我将Genymotion用作仿真器,因为我需要访问某些功能(例如相机)。 I know there are some other solutions. 我知道还有其他解决方案。 It is a little bit hard to debug while emulating but this is the only way i found to access to the camera for now. 在仿真时调试起来有点困难,但这是我目前发现访问相机的唯一方法。 Therefor i can't see what is going on on the second part (Select an image from Gallery). 因此,我看不到第二部分发生了什么(从图库中选择图像)。

$scope.uploadPopup = function() {
    var uploadPopup = $ionicPopup.show({
        title: "Edit profile's picture",
        templateUrl: 'templates/partials/upload-img.html',
        buttons: [
            {
                text: '',
                type: 'button-energized icon-center ion-ios7-camera',
                onTap: function(e) {

                    // e.preventDefault() will stop the popup from closing when tapped.
                    e.preventDefault();
                    alert('Getting camera');
                    Camera.getPicture({
                        quality: 100,
                        saveToPhotoAlbum: false
                    }).then(function(imageURI) {
                        alert(imageURI);
                        $scope.lastPhoto = imageURI;
                    }, function(err) {
                        alert(err);
                    });

                }
            },
            {
                text: 'From gallery',
                type: 'button',
                onTap: function(e) {
                    e.preventDefault();
                    alert('Getting gallery');
                    Camera.getPicture({
                        quality: 100,
                        sourceType: Camera.PictureSourceType.PHOTOLIBRARY
                    }).then(function(imageURI) {
                        alert(imageURI);
                        $scope.lastPhoto = imageURI;
                    }, function(err) {
                        alert(err);
                    });
                }
            }
        ]
    });
};

Service : 服务内容:

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

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

      navigator.camera.getPicture(function(result) {
        // Do any magic you need
        q.resolve(result);
      }, function(err) {
        q.reject(err);
      }, options);

      return q.promise;
    }
  }

}]);

Is it the correct way to do? 这是正确的方法吗? Any hints or ideas? 有任何提示或想法吗?

UPDATE : 更新:

When i add : 当我添加时:

sourceType: Camera.PictureSourceType.CAMERA

to the first function (take a picture from camera). 到第一个功能(从相机拍摄照片)。 It does not work any more. 它不起作用了。 While without (using the default one probably) it does work. 虽然没有(可能使用默认值),但它确实起作用。

When i added the sourceType instead of using the default sourceType(CAMERA) 当我添加sourceType而不是使用默认的sourceType(CAMERA)时

sourceType: Camera.PictureSourceType.CAMERA

It was not working anymore so i guessed something was wrong here. 它不再工作了,所以我猜这里出了点问题。

The correct syntax is : 正确的语法是:

navigator.camera.PictureSourceType.CAMERA

OR (with different option) : 或(具有其他选项):

navigator.camera.PictureSourceType.PHOTOLIBRARY

Not sure why "navigator.camera" and not "Camera", i'm guessing "Camera" is an alias of "navigator.camera". 不知道为什么使用“ navigator.camera”而不是“ Camera”,我猜“ Camera”是“ navigator.camera”的别名。

I think your code is on the right track, like you said it is hard to tell without being able to test it on a device. 我认为您的代码在正确的轨道上,就像您说的那样,如果不能在设备上进行测试就很难分辨。 So i can help you there. 所以我可以帮你。 go grab the intel xdk https://software.intel.com/en-us/html5/tools . 去抢intel xdk https://software.intel.com/zh-cn/html5/tools import your ionic project, then make an account. 导入您的离子项目,然后注册一个帐户。 after you log in go to the test tab and push your app to a test server. 登录后,转到“测试”标签,然后将您的应用推送到测试服务器。 Then install the intel app preview on your phone/tablet (is on android and ios). 然后将intel应用预览安装在您的手机/平板电脑上(在android和ios上)。 open the app, log in, and hit server apps at the bottom, you will see your app and be able to run it on your phone. 打开应用程序,登录并在底部点击服务器应用程序,您将看到您的应用程序并能够在手机上运行它。 You can also use the intel xdk to run an app on your phone with live debugging. 您还可以使用intel xdk通过实时调试在手机上运行应用程序。 Hope this helps! 希望这可以帮助! cheers! 干杯!

Here is the code for my ngCordova plugin: 这是我的ngCordova插件的代码:

//opens up the phones camera in order to take a picture
        //requires module ngCordova and the method $cordovaCamera
        //sets Data.Image to a base 64 string
        $scope.takePhoto = function () {
            document.addEventListener("deviceready", function () {
                var options = {
                    quality: 100,
                    destinationType: Camera.DestinationType.DATA_URL,
                    sourceType: Camera.PictureSourceType.CAMERA,
                    allowEdit: false,
                    encodingType: Camera.EncodingType.PNG,
                    targetWidth: 800,
                    targetHeight: 1100,
                    popoverOptions: CameraPopoverOptions,
                    saveToPhotoAlbum: false
                };
                $cordovaCamera.getPicture(options).then(function (imageData) {

                    $scope.image = "data:image/png;base64," + imageData;
                }, function (err) {
                    // error
                });
            }, false);
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM