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..0f17dc5 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?) { @@ -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, @@ -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") + } } } 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); }