Skip to content

Instantly share code, notes, and snippets.

@julesx
Last active January 15, 2019 12:11
Show Gist options
  • Save julesx/481709acde2d40e6317d to your computer and use it in GitHub Desktop.
Save julesx/481709acde2d40e6317d to your computer and use it in GitHub Desktop.
public class AndroidExternalStorageWriter : IAndroidExternalStorageWriter
{
public string CreateFile(string filename, byte[] bytes)
{
if (!Directory.Exists(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AccountMate")))
Directory.CreateDirectory(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AccountMate"));
var path = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AccountMate", filename);
File.WriteAllBytes(path, bytes);
return path;
}
}
public class DocumentViewer : 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);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Select App"));
}
public DocumentViewer()
{
}
}
public static async void ViewFile(byte[] fileBytes, string filename)
{
var filepath = "";
if (Device.OS == TargetPlatform.Android)
{
var androidWriter = Resolver.Resolve<IAndroidExternalStorageWriter>();
filepath = androidWriter.CreateFile(filename, fileBytes.ToArray());
}
else if (Device.OS == TargetPlatform.iOS)
{
using (var pdfBytes = new MemoryStream(fileBytes))
{
var rootFolder = FileSystem.Current.LocalStorage;
var file = await rootFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
filepath = file.Path;
var newFile = await file.OpenAsync(FileAccess.ReadAndWrite);
using (var outputStream = newFile)
pdfBytes.CopyTo(outputStream);
}
}
if (string.IsNullOrEmpty(filepath))
return;
var documentViewer = Resolver.Resolve<IDocumentViewer>();
var mimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
if (Path.GetExtension(filename).ToLower() == ".pdf")
mimeType = "application/pdf";
try
{
documentViewer.ShowDocumentFile(filepath, mimeType);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
public interface IAndroidExternalStorageWriter
{
string CreateFile(string filename, byte[] bytes);
}
public interface IDocumentViewer
{
void ShowDocumentFile(string filepath, string mimeType);
}
public class DocumentViewer : IDocumentViewer
{
public DocumentViewer()
{
}
public void ShowDocumentFile(string filepath, string mimeType)
{
var fileinfo = new FileInfo(filepath);
var previewController = new QLPreviewController
{
DataSource = new PreviewControllerDataSource(fileinfo.FullName, fileinfo.Name)
};
var controller = FindNavigationController();
controller?.PresentViewController(previewController, true, null);
}
private UINavigationController FindNavigationController()
{
foreach (var window in UIApplication.SharedApplication.Windows)
{
if (window.RootViewController.NavigationController != null)
{
return window.RootViewController.NavigationController;
}
var value = CheckSubs(window.RootViewController.ChildViewControllers);
if (value != null)
return value;
}
return null;
}
private UINavigationController CheckSubs(UIViewController[] controllers)
{
foreach (var controller in controllers)
{
if (controller.NavigationController != null)
{
return controller.NavigationController;
}
var value = CheckSubs(controller.ChildViewControllers);
return value;
}
return null;
}
}
public class DocumentItem : QLPreviewItem
{
private readonly string _uri;
public DocumentItem(string title, string uri)
{
ItemTitle = title;
_uri = uri;
}
public override string ItemTitle { get; }
public override NSUrl ItemUrl => NSUrl.FromFilename(_uri);
}
public class PreviewControllerDataSource : QLPreviewControllerDataSource
{
private readonly string _url;
private readonly string _filename;
public PreviewControllerDataSource(string url, string filename)
{
_url = url;
_filename = filename;
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
return new DocumentItem(_filename, _url);
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return 1;
}
}
@grozdan
Copy link

grozdan commented Mar 21, 2018

Thanks man, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment