1.0.0 • Published 7 months ago

react-native-android-file-handler v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

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

ParameterTypeDescription
fileNamestringName of the file to be downloaded.
filePathstring (optional)Path to the file on the server.
urlstringAPI endpoint for downloading the file.
bodyRecord<string, any>Request body for the API (e.g., for POST requests).
setLoadingDownload(boolean) => voidCallback to indicate loading state.
viewOrDownload'view or 'download'Whether to view the file or save it to the device. Default is 'download'.
folderPathstringDirectory name within the download folder.

Dependencies


Author

Chandannath


Feel free to contribute and raise issues for improvements!