From 02d46f103c944ef5974035cd2ee4e49401eee428 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 25 May 2026 11:18:54 +0000 Subject: [PATCH 1/3] Initial plan From 4a308a83fe535ba4e0b858bad6cc630a8715c21f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 25 May 2026 11:20:44 +0000 Subject: [PATCH 2/3] fix(android): improve docx extension detection and open-document feedback Agent-Logs-Url: https://github.com/fabio21/open_document/sessions/195e7505-5762-40c1-8c86-142af5c22047 Co-authored-by: fabio21 <8049768+fabio21@users.noreply.github.com> --- README.md | 3 +++ .../fsconceicao/open_document/OpenDocument.kt | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1f3bf45..bb874df 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt b/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt index 41ceb56..0109eb3 100644 --- a/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt +++ b/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt @@ -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?) { @@ -26,8 +27,9 @@ 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 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) @@ -43,11 +45,22 @@ class OpenDocument(context: Context, activity: FlutterActivity?) { intent.setDataAndType(Uri.fromFile(File(url)), type) } - this.activity?.startActivity(intent) + if (this.activity == null) { + result.error("Error", "Activity is null", "Open document failure") + return + } + + this.activity?.runOnUiThread { + this.activity?.startActivity(intent) + result.success(null) + } } catch (e: ActivityNotFoundException) { e.printStackTrace() - result.error("Error", e.localizedMessage, "Open document failure") - // Instruct the user to install a PDF reader here, or something + result.error( + "Error", + "No app found to open this file type. Install a compatible viewer.", + "Open document failure" + ) } } From 1089653d66b1ad238927f4d7f08d3b5fb4f0d1c4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 25 May 2026 11:22:51 +0000 Subject: [PATCH 3/3] docs: clarify external app behavior and refine Android open errors Agent-Logs-Url: https://github.com/fabio21/open_document/sessions/195e7505-5762-40c1-8c86-142af5c22047 Co-authored-by: fabio21 <8049768+fabio21@users.noreply.github.com> --- .../fsconceicao/open_document/OpenDocument.kt | 52 +++++++++++-------- lib/open_document.dart | 3 +- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt b/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt index 0109eb3..0f17dc5 100644 --- a/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt +++ b/android/src/main/kotlin/com/fsconceicao/open_document/OpenDocument.kt @@ -26,15 +26,15 @@ class OpenDocument(context: Context, activity: FlutterActivity?) { @RequiresApi(Build.VERSION_CODES.KITKAT) internal fun openDocument(url: String, result: MethodChannel.Result) { - try { - 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") + 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, @@ -44,23 +44,33 @@ class OpenDocument(context: Context, activity: FlutterActivity?) { } else { intent.setDataAndType(Uri.fromFile(File(url)), type) } + } catch (e: Exception) { + e.printStackTrace() + result.error("Error", e.localizedMessage, "Failed to prepare document for opening") + return + } - if (this.activity == null) { - result.error("Error", "Activity is null", "Open document failure") - return - } + val currentActivity = this.activity + if (currentActivity == null) { + result.error("Error", "Activity is null", "Open document failure") + return + } - this.activity?.runOnUiThread { - this.activity?.startActivity(intent) + 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") } - } catch (e: ActivityNotFoundException) { - e.printStackTrace() - result.error( - "Error", - "No app found to open this file type. Install a compatible viewer.", - "Open document failure" - ) } } diff --git a/lib/open_document.dart b/lib/open_document.dart index 984e87c..cc0412f 100644 --- a/lib/open_document.dart +++ b/lib/open_document.dart @@ -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 openDocument({required String filePath}) async { return await OpenDocumentPlatform.instance.openDocument(filePath: filePath); }