2

We need to open MS office & media file (.docx, .xlsx, pptx, .png, .bmp, .mp4, .avi, etc) from local storage of mobile application using Xamarin.Forms

We dont require to open the file inside the application. Just, we need to invoke the respective application in the device for opening its file type.

we have tried: Device.OpenUri(new Uri("https://www.w3.org/TR/PNG/iso_8859-1.txt")) however text file is opening in browser, we want to open these files in their application or atleast a window should appear asking choose the application to open the file.

Note: We are using Xamarin.Forms for Windows, Android & iOS application.

1
  • 2
    You won't be able to get the same experience across all platforms. For Android look at intents. For iOS (and I think Windows as well) look at the custom URL schemes. But especially Apple has enforced a strict policy on detecting which other applications are on a device. So it will take some effort. And I would like to stress; there is no way you are going to do this in a shared code way Commented Dec 10, 2016 at 11:39

2 Answers 2

1

see if this link gets you the answer: https://forums.xamarin.com/discussion/52724/handling-a-file-open-with-or-share-options

You have to use a DependecyService for this. Here's my solution:

//Interface in PCL public interface IDocumentViewer { void ShowDocumentFile(string filepaht, string mimeType); } //Android [assembly: Dependency(typeof(DocumentViewer_Droid))] namespace YourApp.Droid { public class DocumentViewer_Droid : IDocumentViewer { public void ShowDocumentFile(string filepath, string mimeType { var uri = Android.Net.Uri.Parse("file://" + filepath); var intent = new Intent(Intent.ActionView); intent.SetDataAndType(uri, mimeType); intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask); try { Forms.Context.StartActivity(Intent.CreateChooser(intent, "Select App")); } catch(Exception ex) { //Let the user know when something went wrong } } } } //iOS [assembly: Dependency(typeof(DocumentViewer_iOS))] namespace.YourApp.iOS { public class DocumentViewer_iOS : IDocumentViewer { public void ShowDocumentFile(string filepath, string mimeType) { var fileinfo = new FileInfo(filepath); var previewController = new QLPreviewController(); previewController.DataSource = new PreviewControllerDataSource(fileinfo.FullName, fileinfo.Name); UINavigationController controller = FindNavigationController(); if(controller != null) { controller.PresentViewController((UIViewController)previewController, true, (Action)null); } } private UINavigationController FindNavigationController() { foreach(var window in UIApplication.SharedApplication.Windows) { if(window.RootViewController.NavigationController != null) { return window.RootViewController.NavigationController; } else { UINavigationController value = CheckSubs(window.RootViewController.ChildViewControllers); if(value != null) { return value; } } } } private UINavigationController CheckSubs(UIViewController[] controllers) { foreach(var controller in controllers) { if(controller.NavigationController != null) { return controller.NavigationController; } else { UINavigationController value = CheckSubs(controller.ChildViewControllers) if(value != null) { return value; } } return null; } } } public class DocumentItem : QLPreviewItem { private string _title; private string _uri; public DocumentItem(string title, string uri) { _title = title; _uri = uri; } public override string ItemTitle { get { return _title; } } public override NSUrl ItemUrl { get { return NSUrl.FromFilename(_uri)); } } } public class PreviewControllerDataSource : QLPreviewControllerDataSource { private string _url; private string _filename; public PreviewControllerDataSource(string url, string filename) { _url = url; _filename = filename; } public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) { return (IQLPreviewItem)new DocumentItem(_filename, _url); } public override nint PreviewItemCount(QLPreviewController controller) { return (nint)1; } } } 
Sign up to request clarification or add additional context in comments.

Comments

0

I have Binded a library from java.But i haven't test it on IOS so it can't be run.

https://github.com/broteam168/Storage-Chooser-Xamarin

When i use it on Android quiet Good This is demo: Demo storage chooser

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.