From 242e7d633f7658d2a28cfd2af323b3a3b21fc342 Mon Sep 17 00:00:00 2001 From: Kirill Goncharov Date: Mon, 8 Jul 2019 19:29:12 +0300 Subject: [PATCH] feat(android): add support for mediaType option for android devices --- README.md | 2 +- src/imagepicker.android.ts | 15 ++++++++++++++- src/index.d.ts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e69fb4a..475aca3 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ context | prompt | iOS | undefined | Display prompt text when selecting assets. | | numberOfColumnsInPortrait | iOS | 4 | Set the number of columns in Portrait orientation. | | numberOfColumnsInLandscape | iOS | 7 | Set the number of columns in Landscape orientation. | -| mediaType | iOS | Any | Choose whether to pick Image/Video/Any type of assets. | +| mediaType | both | Any (iOS), Image (Android) | Choose whether to pick Image/Video/Any type of assets. | The **hostView** parameter can be set to the view that hosts the image picker. Applicable in iOS only, intended to be used when open picker from a modal page. diff --git a/src/imagepicker.android.ts b/src/imagepicker.android.ts index 8a927e9..14e81d2 100644 --- a/src/imagepicker.android.ts +++ b/src/imagepicker.android.ts @@ -2,6 +2,8 @@ import * as application from "tns-core-modules/application"; import * as imageAssetModule from "tns-core-modules/image-asset"; import * as permissions from "nativescript-permissions"; +import { ImagePickerMediaType } from "."; + class UriHelper { public static _calculateFileUri(uri: android.net.Uri) { let DocumentsContract = (android.provider).DocumentsContract; @@ -126,6 +128,17 @@ export class ImagePicker { return this._options && this._options.mode && this._options.mode.toLowerCase() === 'single' ? 'single' : 'multiple'; } + get mediaType(): string { + const mediaType = this._options && 'mediaType' in this._options ? this._options.mediaType : ImagePickerMediaType.Image; + if (mediaType === ImagePickerMediaType.Image) { + return "image/*"; + } else if (mediaType === ImagePickerMediaType.Video) { + return "video/*"; + } else { + return "*/*"; + } + } + authorize(): Promise { if ((android).os.Build.VERSION.SDK_INT >= 23) { return permissions.requestPermission([(android).Manifest.permission.READ_EXTERNAL_STORAGE]); @@ -194,7 +207,7 @@ export class ImagePicker { let Intent = android.content.Intent; let intent = new Intent(); - intent.setType("image/*"); + intent.setType(this.mediaType); // TODO: Use (android).content.Intent.EXTRA_ALLOW_MULTIPLE if (this.mode === 'multiple') { diff --git a/src/index.d.ts b/src/index.d.ts index 50cf59b..38208e1 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -63,7 +63,7 @@ interface Options { numberOfColumnsInLandscape?: number; /** - * Set the media type (image/video/both) to pick in iOS + * Set the media type (image/video/any) to pick */ mediaType?: ImagePickerMediaType;