首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写在C#中打开将在移动设备上使用默认图像查看器的图像的方法.?

如何编写在C#中打开将在移动设备上使用默认图像查看器的图像的方法.?
EN

Stack Overflow用户
提问于 2016-12-27 03:00:54
回答 1查看 3K关注 0票数 2

为长标题道歉!

我对C#相当陌生(可能只有2-3个月的具体知识),我在大学里学到了.

我一直在试验Xamarin,我想知道如何在代码后面编写一个方法,打开被点击的图像,在Android或iOS的默认图像查看器中打开。

我说Android或iOS是因为我正在做跨平台的Xamarin表单PCL。

谢谢:)节日快乐,新年快乐!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-27 05:38:23

尝试使用以下代码:

PCL接口:

代码语言:javascript
复制
namespace MyApp.Common.Interfaces
{
    public interface IDataViewer
    {
        void showPhoto(string AttachmentName, byte[] AttachmentBytes);
        string ImageExists(string Filename, byte[] ImageData);
    }
}

平台专用(Droid) :

代码语言:javascript
复制
using Android.Content;
using Android.Webkit;
using Android.Widget;
using System;
using System.IO;
using MyApp.Common.Interfaces;
using Xamarin.Forms;

[assembly: Dependency(typeof(DataViewer))]
namespace MyApp.Droid.Common
{
    public class DataViewer : IDataViewer
    {
        public void showPhoto(string AttachmentName, byte[] AttachmentBytes)
        {
            string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path;
            var FileName = AttachmentName;
            Java.IO.File file = new Java.IO.File(dirPath, FileName);

            if (!file.Exists())
            {
                var filename = Path.Combine(dirPath, AttachmentName);
                File.WriteAllBytes(filename, AttachmentBytes);
            }

            Device.BeginInvokeOnMainThread(() =>
            {
                //var oDir = Xamarin.Forms.Forms.Context.FilesDir.AbsolutePath;
                Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
                Intent intent = new Intent(Intent.ActionView);
                String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl((string)uri).ToLower());
                intent.SetDataAndType(uri, mimeType);

                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

                try
                {
                    Xamarin.Forms.Forms.Context.StartActivity(intent);
                }
                catch (System.Exception ex)
                {
                    Toast.MakeText(Xamarin.Forms.Forms.Context, "No Application Available to View this file", ToastLength.Short).Show();
                }
            });
        }

        public string ImageExists(string FileName, byte[] Imagedata)
        {
            string dirPath = Xamarin.Forms.Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).Path;

            Java.IO.File file = new Java.IO.File(dirPath, FileName);

            if (!file.Exists())
            {
                var filename = Path.Combine(dirPath, FileName);
                File.WriteAllBytes(filename, Imagedata);
                return filename;
            }
            else
            {
                var filename = Path.Combine(dirPath, FileName);
                return filename;
            }
        }
    }
}

平台专用(iOS) :

代码语言:javascript
复制
using Foundation;
using QuickLook;
using System;
using System.IO;
using UIKit;
using MyApp.Common.Interfaces;

[assembly: Dependency(typeof(DataViewer))]
namespace MyApp.iOS.Common
{
    public class DataViewer : IDataViewer
    {
        public void showPhoto(string AttachmentName, byte[] AttachmentBytes)
        {
            var FileName = AttachmentName;
            string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            var filename = Path.Combine(dirPath, FileName);
            FileInfo fi = new FileInfo(filename);

            if (!NSFileManager.DefaultManager.FileExists(filename))
            {
                Stream stream = new MemoryStream(AttachmentBytes);
                NSData imgData = NSData.FromStream(stream);
                NSError err;
                imgData.Save(filename, false, out err);
            }

            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                QLPreviewController previewController = new QLPreviewController();
                previewController.DataSource = new PDFPreviewControllerDataSource(fi.FullName, fi.Name);
                UINavigationController controller = FindNavigationController();
                if (controller != null)
                    controller.PresentViewController(previewController, true, null);
            });

        }

        private UINavigationController FindNavigationController()
        {
            foreach (var window in UIApplication.SharedApplication.Windows)
            {
                if (window.RootViewController.NavigationController != null)
                    return window.RootViewController.NavigationController;
                else
                {
                    UINavigationController val = CheckSubs(window.RootViewController.ChildViewControllers);
                    if (val != null)
                        return val;
                }
            }

            return null;
        }

        private UINavigationController CheckSubs(UIViewController[] controllers)
        {
            foreach (var controller in controllers)
            {
                if (controller.NavigationController != null)
                    return controller.NavigationController;
                else
                {
                    UINavigationController val = CheckSubs(controller.ChildViewControllers);
                    if (val != null)
                        return val;
                }
            }
            return null;
        }

        public string ImageExists(string Filename, byte[] Bytedata)
        {

            string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            var filename = Path.Combine(dirPath, Filename);
            FileInfo fi = new FileInfo(filename);

            if (!NSFileManager.DefaultManager.FileExists(filename))
            {
                Stream stream = new MemoryStream(Bytedata);
                NSData imgData = NSData.FromStream(stream);
                NSError err;
                imgData.Save(filename, false, out err);
                return filename;

            }
            else
            {
                return filename;
            }
        }
    }

    public class PDFItem : QLPreviewItem
    {
        string title;
        string uri;

        public PDFItem(string title, string uri)
        {
            this.title = title;
            this.uri = uri;
        }

        public override string ItemTitle
        {
            get { return title; }
        }

        public override NSUrl ItemUrl
        {
            get { return NSUrl.FromFilename(uri); }
        }
    }

    public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
    {
        string url = "";
        string filename = "";

        public PDFPreviewControllerDataSource(string url, string filename)
        {
            this.url = url;
            this.filename = filename;
        }

        public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return (IQLPreviewItem)new PDFItem(filename, url);
        }

        public override nint PreviewItemCount(QLPreviewController controller)
        {
            return 1;
        }
    }
}

使用:

代码语言:javascript
复制
IDataViewer dataViewer = DependencyService.Get<IDataViewer>();
dataViewer.showPhoto(FileName, AttachmentBytes);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41338477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档