0.0.4 • Published 3 years ago

react-native-select-attachment v0.0.4

Weekly downloads
2
License
ISC
Repository
github
Last release
3 years ago

react-native-select-attachment

Supported Versions

react-native-select-attachmentreact-native
>= 0.0.10.60+

Description

react-native-select-attachment allows the selection of photos, videos, or other files (local or cloud storage like Google Drive, Dropbox, Box). The fileName, fileType, and base64 file data are returned to react-native. This is useful if you want to display or store file data from the device into your application.

Getting Started

$ npm install react-native-select-attachment --save

Android

Not supported in the current release, check back for future releases or create a PR

iOS

CocoaPods on iOS needs this extra step

npx pod-install

To use the react-module, you'll first have to write some privacy descriptions for accessing the photo library and the camera. Open your Info.plist as source and populate the usage descriptions as shown below:

<key>NSMicrophoneUsageDescription</key>
<string>For people to hear you during meetings, we need access to your microphone.</string>
<key>NSCameraUsageDescription</key>
<string>This app wants to take pictures.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app wants to use your photos.</string>

Usage

To import into your react app, import the SelectAttachmentButton component.

import SelectAttachment from 'react-native-select-attachment';

And call showPicker as shown below:

var options = {
    maxFileSize: 10,
    fileTypes: ['png', 'jpg', 'pdf'],
    disableCameraPhotos: false,
    disableCameraVideos: false,
    disablePhotos: false,
    disableVideos: false,
    disableFiles: false,
    cameraLabel: 'Camera',
    albumLabel: 'Album',
    filesLabel: 'Files',
    enableImageScaling : true,
    imageScale : 0.90,
    maxImageWidth : 950
};

SelectAttachment.showPicker(options, (res) => {
    if(res.error){
        console.error(res.error);
    } else {
        console.error(res.fileName);
        console.error(res.fileType);
        console.error(res.base64);
    }
});

Reference

Options

maxFileSize

The max file size allowed of the selected file (in MB)

TypeRequiredDefault
StringNonull

fileTypes

The file types that will be allowed for selection (in Files). If left blank all file types are allowed for selection

Supported options are:

  • png
  • pdf
  • jpg
  • csv
TypeRequiredDefault
StringNo'all'

disablePhotos

Disables the Camera from the popup options

TypeRequiredDefault
BooleanNofalse

disableFiles

Disables the Files from the popup options

TypeRequiredDefault
BooleanNofalse

cameraLabel

Overrides the label for Camera popup

TypeRequiredDefault
StringNo'Camera'

albumLabel

Overrides the label for Album popup

TypeRequiredDefault
StringNo'Album'

filesLabel

Overrides the label for Files popup

TypeRequiredDefault
StringNo'Files'

enableImageScaling

Allows scaling images to reduce file size.

TypeRequiredDefault
BooleanNofalse

imageScale

If enableImageScaling is true, the image resolution is reduced by a percentage entered. Example 0.5 would reduce the resolution by 50%

TypeRequiredDefault
DecimalNo1.0

maxImageWidth

The max width of the image in pixels. Images will be scaled to the entered value. This is useful to reduce file size.

TypeRequiredDefault
IntegerNo950