react-native-android-file-handler

1.0.0 • Public • Published

react-native-android-file-handler

A utility to handle file downloads on Android devices in React Native applications. Supports downloading, viewing, and managing files like PDFs, images, videos, and more.


Features

  • Download files from a URL or a given path.
  • Support for viewing files directly.
  • Automatic MIME type detection based on file extensions.
  • Saves files in a specified folder in the Android device's download directory.
  • Handles Bearer token authentication for secured APIs.
  • Creates directories automatically if they don't exist.
  • Toast notifications for successful downloads.

Installation

Ensure you have the following dependencies installed in your React Native project:

npm install react-native-android-file-handler @react-native-async-storage/async-storage react-native-blob-util

Link react-native-blob-util to your project:

npx react-native link react-native-blob-util

For Android, follow the react-native-blob-util setup guide.


Setup Permissions for Android

Ensure the following permissions are added to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

For Android 11 and above, add scoped storage handling as required.


Usage

Importing the Function

import { downloadFile } from 'react-native-android-file-handler';

Example Usage

const handleFileDownload = () => {
  downloadFile({
    fileName: 'example.pdf',
    url: 'https://example.com/api/download',
    folderPath: 'MyAppDownloads',
    body: { id: 12345 },
    viewOrDownload: 'download', // 'view' or 'download'
    setLoadingDownload: (loading) => console.log('Loading:', loading),
  });
};

Parameters

Parameter Type Description
fileName string Name of the file to be downloaded.
filePath string (optional) Path to the file on the server.
url string API endpoint for downloading the file.
body Record<string, any> Request body for the API (e.g., for POST requests).
setLoadingDownload (boolean) => void Callback to indicate loading state.
viewOrDownload 'view or 'download' Whether to view the file or save it to the device. Default is 'download'.
folderPath string Directory name within the download folder.

Dependencies


Author

Chandannath


Feel free to contribute and raise issues for improvements!

Package Sidebar

Install

npm i react-native-android-file-handler

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

8.48 kB

Total Files

4

Last publish

Collaborators

  • chandan_nath