Get picture from gallery ( ionic 2 | typescript )


#1

Hello , any suggestion to get picture from gallery using ionic-native package ?

Thanks in advance :slight_smile:


#2

Simple:

first install camera plugin

ionic plugin add cordova-plugin-camera

Than try next:

let options = {
    destinationType: Camera.DestinationType.DATA_URL,
    sourceType: Camera.PictureSourceType.PHOTOLIBRARY
};

navigator.camera.getPicture(
    (data)  => {
      let image = "data:image/jpeg;base64," + data;
    },
    (error) => {},
    options
);

#3

Hi,
as https://github.com/apache/cordova-plugin-camera some options values change to number like
destinationType: 0, sourceType: 0, encodingType: 0, quality:70, allowEdit: true, saveToPhotoAlbum: false
if ı not false remember this optinons for select item.


#4

@gencelo

For example Camera.DestinationType.DATA_URL is constant which is equal 0 so it is the same thing, only Camera.DestinationType.DATA_URL is more readable than just 0

So, if you ask me, you should use Camera.DestinationType.DATA_URL


#5

in plugin codes destinationType uses as number so if you write string it mat be return error.
i agree with you string is more readble.


#6

I don’t see any strings in Camera.DestinationType.DATA_URL

If you do console.log(typeof Camera.DestinationType.DATA_URL); as result you will get number not string


#7

thank for reply , this is how to take a new pic , and i need to get existing image from gallery


#8

It’s not, notice that the sourceType is set to the Photo Library. The Camera plugin can pull in a picture either from the camera, or from the users photo library.