Skip to content

feat(android): add support for mediaType option for android devices #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 14 additions & 1 deletion src/imagepicker.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<any>android.provider).DocumentsContract;
Expand Down Expand Up @@ -143,6 +145,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<void> {
if ((<any>android).os.Build.VERSION.SDK_INT >= 23) {
return permissions.requestPermission([(<any>android).Manifest.permission.READ_EXTERNAL_STORAGE]);
Expand Down Expand Up @@ -211,7 +224,7 @@ export class ImagePicker {

let Intent = android.content.Intent;
let intent = new Intent();
intent.setType("image/*");
intent.setType(this.mediaType);

// TODO: Use (<any>android).content.Intent.EXTRA_ALLOW_MULTIPLE
if (this.mode === 'multiple') {
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down