Preview 2.0 is now in Public Beta!
Read the Announcement

@nativescript/imagepicker

Imagepicker plugin supporting both single and multiple selection. As of 2.0, it returns rich information about your selection including filesize, path, video duration and thumbnails etc.
Plugin supports iOS8+ and uses QBImagePicker cocoapod.
For Android it uses Intents to open the stock images or file pickers. For Android 6 (API 23) and above, the permissions to read file storage should be explicitly required.


Note: Version 2.0 contains breaking changes. In order supply more information about your selection, the ImageSource asset is nested in the response so you'll need to update your code to use result.asset instead of result as your src for your Images.

Table of Contents

Installation

npm install @nativescript/imagepicker

Required Permissions

Android

Add the following permissions to the App_Resources/Android/src/main/AndroidManifest.xml file:

  • targetSdkVersion < 33
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
  • targetSdkVersion >=33(Android 13+)
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

See the complete example here.

iOS

Using the plugin on iOS requires the NSPhotoLibraryUsageDescription permission. Modify the app/App_Resources/iOS/Info.plist file to add it as follows:

<key>NSPhotoLibraryUsageDescription</key>
<string>Description text goes here</string>

Apple App Store might reject your app if you do not describe why you need this permission. The default message Requires access to photo library. might not be enough for the App Store reviewers.

Usage

You can play with the plugin on StackBlitz at the following links:

Importing the plugin

import * as imagePickerPlugin from '@nativescript/imagepicker'
var imagePickerPlugin = require('@nativescript/imagepicker')

Creating the Imagepicker instance

Create an Imagepicker instance in single or multiple mode to specifiy if the image picker will be used for single or multiple selection.

let imagePickerObj: ImagePicker = imagePickerPlugin.create({
  mode: 'single'
})
var imagePickerObj = imagePickerPlugin.create({ mode: 'single' })

Using the ImagePicker Instance

Once you've created the picker instance, use it to request for permissions, present the list of media assets to be picked from, and process the selection.

imagePickerObj
  .authorize()
  .then(function () {
    return imagePickerObj.present()
  })
  .then(function (selection) {
    selection.forEach(function (selected) {
      this.imageSource = selected.asset
      this.type = selected.type
      this.filesize = selected.filesize
      //etc
    })
    list.items = selection
  })
  .catch(function (e) {
    // process error
  })

Note

To request permissions for Android 6+ (API 23+), use @nativescript-community/perms plugin.

API

ImagePicker

MethodReturnsDescription
constructor(options: Options)ImagePickerInstanciates the ImagePicker class with the optional options parameter. See Options
authorize()Promise<void>Requests the required permissions. Call it before calling present(). In case of a failed authorization, consider notifying the user for degraded functionality.
present()Promise<ImageAsset[]>Presents the image picker UI.

create()

imagePicker: ImagePicker = imagePickerPlugin.create(options: Options, hostView: View)

Creates an instance of the ImagePicker class.

  • Optional: options - The ImagePicker instance settings. See Options
  • Optional: (iOS-only) hostView - Can be set to the view that hosts the image picker. Intended to be used when opening the picker from a modal page.

Response

As of version 2.0, imagepicker returns more information about your selection.

;[
  {
    asset: {}, // ImageSource. this is what you'll use to set the src of an Image for example.
    type: 'image', // either 'video' or 'image'
    filename: 'mycoolfile-0.jpg', // the filename
    originalFilename: 'IMG1001.JPG', // the original filename (may be useful if you changed the filename when copying)
    path: '/myapp/media/mycoolfile-0.jpg', // the path where the file is
    duration: 100, // video only, the duration in seconds.
    thumbnail: {} // ImageSource, video only. This is what you'll use to set the src of an Image for example.
  }
]

Options

OptionTypeDefaultDescription
modestringmultipleThe mode of the imagepicker. Possible values are single for single selection and multiple for multiple selection.
minimumNumberOfSelectionnumber0Optional: (iOS-only) The minumum number of selected assets.
maximumNumberOfSelectionnumber0Optional: (iOS-only) The maximum number of selected assets.
showsNumberOfSelectedAssetsbooleantrueOptional: (iOS-only) Display the number of selected assets.
promptstringundefinedOptional: (iOS-only) Display prompt text when selecting assets.
numberOfColumnsInPortraitnumber4Optional: (iOS-only) Sets the number of columns in Portrait orientation
numberOfColumnsInLandscapenumber7Optional: (iOS-only) Sets the number of columns in Landscape orientation.
mediaTypeImagePickerMediaTypeAnyOptional: The type of media asset to pick whether to pick Image/Video/Any type of assets.
copyToAppFolderstringundefinedOptional: If passed, a new folder will be created in your applications folder and the asset will be copied there.
renameFileTostringundefinedOptional: If passed, the copied file will be named what you choose. If you select multiple, -index will be appended.
showAdvanced booleanfalseOptional😦Android-only) Show internal and removable storage options on Android (WARNING: not supported officially).
android{read_external_storage: string;}Optional: (Android-only) Provides a reason for permission request to access external storage on API level above 23.

ImagePickerMediaType

  • Any = 0,
  • Image = 1,
  • Video = 2

License

Apache License Version 2.0