コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
namespace AppName.Services
{
//DependencyServiceから利用する
public interface IFileService
{
void Open(string filePath, ref string err);
}
}
using Xamarin.Forms;
using AppName.Droid.Services;
using AppName.Services;
[assembly: Dependency(typeof(FileService))]
namespace AppName.Droid.Services
{
public class FileService : IFileService
{
public void Open(string filePath, ref string err)
{
try
{
//インテント
Intent intent = new Intent();
//MimeTypeの取得
string mimetype = FileService.GetMimeType(filePath);
if (String.IsNullOrEmpty(mimetype))
{
//MimeTypeが取得できなかった場合
//アクションにACTION_SENDを指定して暗黙的インテントを呼び出すことで、
//インストールされているアプリで対応可能なものが列挙されます。
mimetype = "*/*";
intent.SetAction(Intent.ActionSend);
}
else
{
//MimeTypeが取得できた場合
intent.SetAction(Intent.ActionView);
}
//URLとmimetypeを取得
Java.IO.File file = new Java.IO.File(filePath);
Android.Net.Uri uri = null;
if ((int)Build.VERSION.SdkInt < (int)(BuildVersionCodes.N))
{
//Android6.0以前
uri = Android.Net.Uri.FromFile(file);
}
else
{
//Android7.0(Nougat)以降から10.0(Q)未満まで
uri = Android.Support.V4.Content.FileProvider.GetUriForFile(
Forms.Context,
Forms.Context.PackageName + ".fileprovider",
file
);
}
intent.SetDataAndType(uri, mimetype);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
// open
Forms.Context.StartActivity(intent);
}
catch (ActivityNotFoundException ex)
{
err = ex.Message;
}
}
}
}
using QuickLook;
public class QLPreviewItemBundle : QLPreviewItem
{
string _fileName = String.Empty;
string _filePath = String.Empty;
public QLPreviewItemBundle(string fileName, string filePath)
{
_fileName = fileName;
_filePath = filePath;
}
public override string ItemTitle
{
get
{
return _fileName;
}
}
public override NSUrl ItemUrl
{
get
{
var documents = NSBundle.MainBundle.BundlePath;
var lib = Path.Combine(documents, _filePath);
var url = NSUrl.FromFilename(lib);
return url;
}
}
}
using QuickLook;
public class PreviewControllerDS : QLPreviewControllerDataSource
{
private IQLPreviewItem _item;
public PreviewControllerDS(IQLPreviewItem item)
{
_item = item;
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return 1;
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
return _item;
}
}
using QuickLook;
using Xamarin.Forms;
using AppName.iOS.Services;
using AppName.Services;
[assembly: Dependency(typeof(FileService))]
namespace AppName.iOS.Services
{
public class FileService : IFileService
{
public void Open(string filePath, ref string err)
{
if (String.IsNullOrEmpty(filePath))
{
return;
}
FileInfo fi = new FileInfo(filePath);
//QuickLook
QLPreviewItemBundle prevItem = new QLPreviewItemBundle(fi.Name, fi.FullName);
QLPreviewController previewController = new QLPreviewController();
previewController.DataSource = new PreviewControllerDS(prevItem);
UINavigationController controller = FindNavigationController();
if (controller != null)
{
controller.PresentViewController(previewController, true, null);
}
else
{
UINavigationController nav = new UINavigationController(previewController);
UIApplication.SharedApplication.Windows[1].RootViewController.PresentViewController(
nav, true, null);
}
}
private UINavigationController FindNavigationController()
{
//Check to see if the rootviewcontroller is the navigationcontroller.
foreach (var window in UIApplication.SharedApplication.Windows)
{
if (window.RootViewController == null)
{
continue;
}
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;
}
}
}
using AppName.Services;
using Xamarin.Forms;
public partial class TestPage : ContentPage
{
async void OnFileOpenClick()
{
string err = String.Empty;
string filePath = "/file.txt";
DependencyService.Get<IFileService>().Open(filePath, ref err);
if (!String.IsNullOrEmpty(err))
{
await myPage.DisplayAlert("ファイルを開く", err, "OK");
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。