nativescript-imagepicker: selected.getImage is not a function

I’m making an app where some data are stored in a SQL database. But in order to store images and videos I need to get the file uri first. I tried everything I could but nothing worked.

This is how my JS file looks like:

var imagepicker = require("nativescript-imagepicker");
var permissions = require( "nativescript-permissions" );
var ImageSourceModule = require("image-source");

function openImage() { 
   var context = imagepicker.create({ mode: "single" }); // use "multiple" for multiple selection
        context
    .authorize()
    .then(function() {
        return context.present();
    })
    .then(function(selection) {
        selection.forEach(function(selected) {
            selected.getImage().then((source) => {
                    console.log(selected.fileUri); 
                });     
        });  
    }).catch(function (e) {
        alert(e);// process error
    });
}


exports.openImage = openImage;

I can select images without a problem but then I get this error “selected.getImage is not a function”.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

I tested this using the latest (6.0.1) version of the image picker plugin. For Android, getting the image path is very easy. Assuming selection is the array of image assets returned from the image picker:

selection.forEach(function(selected) {
     console.log(selected.android.toString()); 
});

For iOS, it is a bit more tricky as the native PHAsset object does not have a property containing the path:

selection.forEach(function(selected) {
    const ios = selected.ios;
    if (ios && ios.mediaType === PHAssetMediaType.Image) {
        const opt = PHImageRequestOptions.new();
        opt.version = PHImageRequestOptionsVersion.Current;
        PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
            ios, opt, (imageData: NSData, dataUTI: string, orientation: UIImageOrientation, info: NSDictionary<any, any>) => {
                console.log(info.objectForKey("PHImageFileURLKey").toString());
            });
    }
});

You need to add the tns-platform-declarations dependency to your app. It contains the definitions for all native objects like NSData and UIImageOrientation.

Working solution

const ios: PHAsset = selected.ios;
if (ios && ios.mediaType === PHAssetMediaType.Image) {
  filePath = await new Promise((resolve, reject) => {
    ios.requestContentEditingInputWithOptionsCompletionHandler(
      null,
      (input, info) => {
        resolve(input.fullSizeImageURL.toString().replace("file://", ""));
      }
    );
  });
}

Please provide good documentation for last two versions.

Sorry, to start the question here again, but how can I get the path to a Video? I’ve tried to figure it out by myself, but I stuck. What I have so far:

imagepicker.create({
          maximumNumberOfSelection: 1,
          minimumNumberOfSelection: 1,
          mediaType: imagepicker.ImagePickerMediaType.Video,
          mode: 'single',
          prompt: 'Bitte Video auswählen'
        }).present()
          .then(
            (videoAsset: any) => {
              const selected = videoAsset[0];
              const ios = selected.ios;

              if (ios) {
                const opt = PHVideoRequestOptions.new();
                opt.version = PHVideoRequestOptionsVersion.Current;

                PHImageManager.defaultManager().requestAVAssetForVideoOptionsResultHandler(
                  ios, opt, (asset: AVAsset, audioMix: AVAudioMix, info: NSDictionary<any, any>) => {
                    console.log(info.objectForKey("PHImageFileURLKey").toString());
                  });
              }
            },
            (rej) => {
              console.log(rej);
            });

I have the Video Player plugin installed and want to play the selected video in the app as a preview and later upload it to firebase.

But I can’t get this thing working. Any ideas / solutions?