file-formats-xamarin-demos/PDF/PDF.Android/SaveAndroid.cs

123 строки
4.3 KiB
C#

#region Copyright Syncfusion Inc. 2001-2024
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.IO;
using Android.Content;
using Java.IO;
using Xamarin.Forms;
using SampleBrowser.PDF.Droid;
using AndroidX.Core.Content;
[assembly: Dependency(typeof(SaveAndroid))]
[assembly: Dependency(typeof(MailService))]
namespace SampleBrowser.PDF.Droid
{
internal class SaveAndroid : ISave
{
public void Save(string fileName, string contentType, MemoryStream stream)
{
string exception = string.Empty;
string root = null;
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
}
else
{
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
if (file.Exists())
{
file.Delete();
}
try
{
FileOutputStream outs = new FileOutputStream(file);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception e)
{
exception = e.ToString();
}
if (file.Exists() && contentType != "application/html")
{
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
Android.Net.Uri path = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
intent.SetDataAndType(path, mimeType);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
}
}
public class MailService : IMailService
{
public MailService()
{
}
public void ComposeMail(string fileName, string[] recipients, string subject, string messagebody, MemoryStream filestream)
{
string exception = string.Empty;
string root = null;
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
{
root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
}
Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, fileName);
if (file.Exists())
{
file.Delete();
}
try
{
FileOutputStream outs = new FileOutputStream(file);
outs.Write(filestream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception e)
{
exception = e.ToString();
}
Intent email = new Intent(Android.Content.Intent.ActionSend);
Android.Net.Uri uri = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
email.PutExtra(Android.Content.Intent.ExtraSubject, subject);
email.PutExtra(Intent.ExtraStream, uri);
email.SetType("application/pdf");
Forms.Context.StartActivity(email);
}
}
}