|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'; |
| 3 | + |
| 4 | +export interface ChooserResult { |
| 5 | + data: Uint8Array; |
| 6 | + dataURI: string; |
| 7 | + mediaType: string; |
| 8 | + name: string; |
| 9 | + uri: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * @name Chooser |
| 14 | + * @description |
| 15 | + * File chooser plugin for Cordova. |
| 16 | + * |
| 17 | + * The following must be added to config.xml to prevent crashing when selecting large files on Android: |
| 18 | + * ```xml |
| 19 | + * <platform name="android"> |
| 20 | + * <edit-config |
| 21 | + * file="app/src/main/AndroidManifest.xml" |
| 22 | + * mode="merge" |
| 23 | + * target="/manifest/application"> |
| 24 | + * <application android:largeHeap="true" /> |
| 25 | + * </edit-config> |
| 26 | + * </platform> |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * @usage |
| 30 | + * ```typescript |
| 31 | + * import { Chooser } from '@ionic-native/chooser'; |
| 32 | + * |
| 33 | + * |
| 34 | + * constructor(private chooser: Chooser) { } |
| 35 | + * |
| 36 | + * ... |
| 37 | + * |
| 38 | + * |
| 39 | + * this.chooser.getFile() |
| 40 | + * .then(file => console.log(file ? file.name : 'canceled')) |
| 41 | + * .catch((error: any) => console.error(error)); |
| 42 | + * |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * @interfaces |
| 46 | + * ChooserResult |
| 47 | + */ |
| 48 | +@Plugin({ |
| 49 | + pluginName: 'Chooser', |
| 50 | + plugin: 'cordova-plugin-chooser', |
| 51 | + pluginRef: 'chooser', |
| 52 | + repo: 'https://github.com/cyph/cordova-plugin-chooser', |
| 53 | + platforms: ['Android', 'iOS'] |
| 54 | +}) |
| 55 | +@Injectable() |
| 56 | +export class Chooser extends IonicNativePlugin { |
| 57 | + /** |
| 58 | + * Displays native prompt for user to select a file. |
| 59 | + * @param {string} [accept] Optional MIME type filter (e.g. 'image/gif,video/*'). |
| 60 | + * @return {Promise<any>} Promise containing selected file's raw binary data, |
| 61 | + * base64-encoded data: URI, MIME type, display name, and original URI. |
| 62 | + */ |
| 63 | + @Cordova() |
| 64 | + getFile(accept: string): Promise<ChooserResult | undefined> { |
| 65 | + return; |
| 66 | + } |
| 67 | +} |
0 commit comments