gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:32:31 24 4
gpt4 key购买 nike

为长标题道歉!

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

我一直在试验 Xamarin Forms XAML,我想知道如何在代码隐藏中编写一个方法来打开点击的图像,以便在 Android 或 iOS 的默认图像查看器中打开。

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

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

最佳答案

试试下面的代码:

PCL 接口(interface):

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<IDataViewer>();
dataViewer.showPhoto(FileName, AttachmentBytes);

关于c# - 如何编写一个在 C# 中打开图像的方法,该方法将使用移动设备上的默认图像查看器...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com