๐Ÿ“‚ open_file_custom

A Flutter plugin to open files natively using Android intents with MIME type support. Supports opening .pdf, .doc, .xls, .jpg, .png, and many more directly from the file path โ€” even from copied asset files.


โœจ Features

โœ… Open local files with correct MIME types
โœ… Handles missing apps gracefully (NO_APP result)
โœ… Uses Android native intents (via Java)
โœ… Support for Android 7.0+ FileProvider
โœ… Clean error handling: FILE_NOT_FOUND, OPEN_ERROR
โœ… Dart wrapper for ease of use
โœ… Built for extendability


๐Ÿ”ง Installation

Add this to your pubspec.yaml:

dependencies:
  open_file_custom: ^0.0.1

Then run:

flutter pub get

๐Ÿ› ๏ธ Android Setup

In AndroidManifest.xml, add inside <application>:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

Create the file android/app/src/main/res/xml/file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
    <files-path name="files" path="." />
    <cache-path name="cache" path="." />
</paths>

๐Ÿš€ Usage

import 'package:open_file_custom/open_file_custom.dart';

final openFilePlugin = OpenFileCustom();

final result = await openFilePlugin.openFile(
  filePath: '/storage/emulated/0/Download/sample.pdf',
  mimeType: 'application/pdf',
);

if (result == "opened") {
  print("File opened successfully.");
} else if (result == "NO_APP") {
  print("No app found to open this file type.");
}

๐Ÿ“ฆ Example

Open a PDF from assets (with path_provider and rootBundle):

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

Future<void> openAssetFile(String assetPath, String fileName) async {
  final bytes = await rootBundle.load(assetPath);
  final dir = await getTemporaryDirectory();
  final file = File('${dir.path}/$fileName');
  await file.writeAsBytes(bytes.buffer.asUint8List());

  await OpenFileCustom().openFile(filePath: file.path, mimeType: 'application/pdf');
}

๐Ÿงช Result Codes

Result Meaning
opened File opened successfully
NO_APP No app found to open the file
FILE_NOT_FOUND File path doesn't exist
OPEN_ERROR An error occurred during opening

๐Ÿงฉ Platform Support

Platform Support
Android โœ… Yes
iOS โŒ Not yet

iOS support coming soon. PRs welcome!


๐Ÿ“œ License

This project is licensed under the MIT License.


โœ๏ธ Author

Maintained by Sudipta Samanta ([email protected]).
Feel free to contribute, suggest features, or fork the project.