๐ 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.