Xamarin.Forms - 如何打开存储在本地存储中的文件

时间:2016-12-10 10:04:21

标签: xamarin xamarin.forms

我们需要开设MS office&使用Xamarin.Forms从移动应用程序的本地存储中获取媒体文件(.docx,.xlsx,pptx,.png,.bmp,.mp4,.avi等)

我们不需要在应用程序内打开文件。只是,我们需要在设备中调用相应的应用程序来打开其文件类型。

我们尝试过: Device.OpenUri(new Uri("https://www.w3.org/TR/PNG/iso_8859-1.txt"))但是文本文件在浏览器中打开,我们想在他们的应用程序中打开这些文件,或者至少应该出现一个窗口,要求选择要打开文件的应用程序。

注意:我们正在使用Xamarin.Forms for Windows,Android& iOS应用程序。

2 个答案:

答案 0 :(得分:1)

看看这个链接是否能为您提供答案: https://forums.xamarin.com/discussion/52724/handling-a-file-open-with-or-share-options

  

您必须为此使用DependecyService。这是我的解决方案:

//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; }
}
}

答案 1 :(得分:0)

我从Java绑定了一个库。但是我没有在IOS上对其进行测试,因此无法运行。

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

当我在Android上安静地使用它时好 这是演示: Demo storage chooser