Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Used to create a folder on the user's mobile phone and Desktop;

---
## Opening pdf, xlsx, docs, ppt and zip files

> On Android and Windows this plugin uses apps installed on the device/system to open files.
> For `.docx` and other formats, make sure a compatible viewer is installed.
---

## Getting Started
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.core.content.FileProvider
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import java.io.File
import java.util.Locale


class OpenDocument(context: Context, activity: FlutterActivity?) {
Expand All @@ -25,14 +26,15 @@ class OpenDocument(context: Context, activity: FlutterActivity?) {

@RequiresApi(Build.VERSION_CODES.KITKAT)
internal fun openDocument(url: String, result: MethodChannel.Result) {
try {

val type = getFileType(name(url).split(".")[1])
val intent = Intent(Intent.ACTION_VIEW)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
intent.addCategory("android.intent.category.DEFAULT")
val fileName = name(url)
val extension = fileName.substringAfterLast('.', "").lowercase(Locale.ROOT)
val type = if (extension.isNotEmpty()) getFileType(extension) else "*/*"
val intent = Intent(Intent.ACTION_VIEW)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
intent.addCategory("android.intent.category.DEFAULT")

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val uri = FileProvider.getUriForFile(
applicationContext,
Expand All @@ -42,12 +44,33 @@ class OpenDocument(context: Context, activity: FlutterActivity?) {
} else {
intent.setDataAndType(Uri.fromFile(File(url)), type)
}

this.activity?.startActivity(intent)
} catch (e: ActivityNotFoundException) {
} catch (e: Exception) {
e.printStackTrace()
result.error("Error", e.localizedMessage, "Open document failure")
// Instruct the user to install a PDF reader here, or something
result.error("Error", e.localizedMessage, "Failed to prepare document for opening")
return
}

val currentActivity = this.activity
if (currentActivity == null) {
result.error("Error", "Activity is null", "Open document failure")
return
}

currentActivity.runOnUiThread {
try {
currentActivity.startActivity(intent)
result.success(null)
} catch (e: ActivityNotFoundException) {
e.printStackTrace()
result.error(
"Error",
"No app found to open this file type. Install a compatible viewer.",
"Open document failure"
)
} catch (e: Exception) {
e.printStackTrace()
result.error("Error", e.localizedMessage, "Open document failure")
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/open_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'open_document_platform_interface.dart';
///- It has the power to open the zip creating a new folder with
/// the name of the file using the plugin [Archive]
class OpenDocument {
/// - Open the document by the indicated path [filePath],
/// - Open the document by the indicated path [filePath].
/// - On Android and Windows, this dispatches the file to a compatible installed app.
static Future<void> openDocument({required String filePath}) async {
return await OpenDocumentPlatform.instance.openDocument(filePath: filePath);
}
Expand Down