【问题标题】:Xamarin.Forms - How to open files stored in local storageXamarin.Forms - 如何打开存储在本地存储中的文件
【发布时间】:2017-04-25 18:01:52
【问题描述】:

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

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

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

注意:我们正在为 Windows、Android 和 iOS 应用程序使用 Xamarin.Forms。

【问题讨论】:

  • 您将无法在所有平台上获得相同的体验。对于 Android,请查看意图。对于 iOS(我认为 Windows 也是如此),请查看自定义 URL 方案。但尤其是苹果公司在检测设备上还有哪些其他应用程序方面实施了严格的政策。所以需要一些努力。我想强调一下;您无法以共享代码的方式执行此操作

标签: xamarin xamarin.forms


【解决方案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; }
}
}

【讨论】:

    【解决方案2】:

    我已经从java绑定了一个库。但是我没有在IOS上测试它所以它不能运行。

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 2020-06-09
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多