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

为长标题道歉!

我在大学里学到的C#(可能只有2-3个月的具体知识)相当新…

我一直在尝试使用Xamarin Forms XAML,我想知道如何在代码隐藏中编写一个方法,打开要在Android或iOS的默认图像查看器中打开的抽头图像。

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

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

请尝试以下代码:

PCL界面:

namespace MyApp.Common.Interfaces { public interface IDataViewer { void showPhoto(string AttachmentName, byte[] AttachmentBytes); string ImageExists(string Filename, byte[] ImageData); } } 

平台特定(Droid):

 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):

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

用法:

 IDataViewer dataViewer = DependencyService.Get(); dataViewer.showPhoto(FileName, AttachmentBytes);