Skip to content

Commit 242e7d6

Browse files
committed
feat(android): add support for mediaType option for android devices
1 parent b8a66e6 commit 242e7d6

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ context
133133
| prompt | iOS | undefined | Display prompt text when selecting assets. |
134134
| numberOfColumnsInPortrait | iOS | 4 | Set the number of columns in Portrait orientation. |
135135
| numberOfColumnsInLandscape | iOS | 7 | Set the number of columns in Landscape orientation. |
136-
| mediaType | iOS | Any | Choose whether to pick Image/Video/Any type of assets. |
136+
| mediaType | both | Any (iOS), Image (Android) | Choose whether to pick Image/Video/Any type of assets. |
137137

138138
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.
139139

src/imagepicker.android.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as application from "tns-core-modules/application";
22
import * as imageAssetModule from "tns-core-modules/image-asset";
33
import * as permissions from "nativescript-permissions";
44

5+
import { ImagePickerMediaType } from ".";
6+
57
class UriHelper {
68
public static _calculateFileUri(uri: android.net.Uri) {
79
let DocumentsContract = (<any>android.provider).DocumentsContract;
@@ -126,6 +128,17 @@ export class ImagePicker {
126128
return this._options && this._options.mode && this._options.mode.toLowerCase() === 'single' ? 'single' : 'multiple';
127129
}
128130

131+
get mediaType(): string {
132+
const mediaType = this._options && 'mediaType' in this._options ? this._options.mediaType : ImagePickerMediaType.Image;
133+
if (mediaType === ImagePickerMediaType.Image) {
134+
return "image/*";
135+
} else if (mediaType === ImagePickerMediaType.Video) {
136+
return "video/*";
137+
} else {
138+
return "*/*";
139+
}
140+
}
141+
129142
authorize(): Promise<void> {
130143
if ((<any>android).os.Build.VERSION.SDK_INT >= 23) {
131144
return permissions.requestPermission([(<any>android).Manifest.permission.READ_EXTERNAL_STORAGE]);
@@ -194,7 +207,7 @@ export class ImagePicker {
194207

195208
let Intent = android.content.Intent;
196209
let intent = new Intent();
197-
intent.setType("image/*");
210+
intent.setType(this.mediaType);
198211

199212
// TODO: Use (<any>android).content.Intent.EXTRA_ALLOW_MULTIPLE
200213
if (this.mode === 'multiple') {

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ interface Options {
6363
numberOfColumnsInLandscape?: number;
6464

6565
/**
66-
* Set the media type (image/video/both) to pick in iOS
66+
* Set the media type (image/video/any) to pick
6767
*/
6868
mediaType?: ImagePickerMediaType;
6969

0 commit comments

Comments
 (0)