Skip to content

Commit

Permalink
Add permanent file saving strategy for Android
Browse files Browse the repository at this point in the history
Recap: The MAUI community toolkit filesaver requires direct file system
access which has been disallowed in Android 13 (API 33) beyond a handful
of media folders (Photos, Videos, Music) OR if your app requests the
high risk permission that google requires you to justify. I stuck a
workaround in to forcibly target a lower framework version but as of
late August google stopped anyone publishing updates using that
workaround (which I knew was coming).

After months waiting and googling, it appears that the Storage Access
Framework is already readily available using the
[Share Api](https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?tabs=android#share-a-file)

That was very frustrating to find out or search for. I'm still utilising
the community toolkit file saver on other platforms but since Android
only allows media folder access it makes sense to save "Documents" using
the Storage Access Framework so that users can effectively "share" the
file with another app (like Google Drive, Gmail or a file manager) in
order to more natively "Save" their data somewhere else.

What a pain.
  • Loading branch information
Rory-Reid committed Oct 8, 2023
1 parent 9f9d0ab commit 3e38d8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Basemix/Pages/RatProfile.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Runtime.CompilerServices;
using Basemix.Lib;
using Basemix.Lib.Litters;
using Basemix.Lib.Litters.Persistence;
Expand Down Expand Up @@ -167,11 +166,21 @@ public async Task ExportPdfPedigree()
this.LitterName,
this.PedigreeContext.FooterText,
this.PedigreeContext.ShowSex);
var stream = new MemoryStream();
this.PdfGenerator.WriteToStream(pdf, stream);

#if ANDROID
var tempFilePath = Path.Combine(FileSystem.Current.CacheDirectory, $"{this.Rat.Name}.pdf");
await using var tempFile = new FileStream(tempFilePath, FileMode.Create);
this.PdfGenerator.WriteToStream(pdf, tempFile);
await Share.Default.RequestAsync(new ShareFileRequest
{
Title = "Save pedigree",
File = new ShareFile(tempFilePath)
});
#else
try
{
var stream = new MemoryStream();
this.PdfGenerator.WriteToStream(pdf, stream);
await FileSaver.Default.SaveAsync($"{this.Rat.Name}.pdf", stream, CancellationToken.None);
}
catch (FolderPickerException e) when (e.Message is "Operation cancelled.")
Expand All @@ -186,6 +195,7 @@ public async Task ExportPdfPedigree()
// Potential bug in CommunityToolkit for android. Seems to be an issue with returning the filepath
// and not with actually saving the file. I can safely ignore it for now.
}
#endif
}
catch (Exception e)
{
Expand Down
12 changes: 11 additions & 1 deletion src/Basemix/Pages/Settings.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@page "/settings"
@using Basemix.Db
@using System.IO
@using System.Net.Security
@using Basemix.Lib
@using CommunityToolkit.Maui.Storage
@inject GetDataDirectory GetDataDirectory;
Expand Down Expand Up @@ -94,6 +93,16 @@
try
{
await using var file = new FileStream(dbPath, FileMode.Open);
#if ANDROID
var tempFilePath = Path.Combine(FileSystem.Current.CacheDirectory, "basemix.sqlite3");
await using var tempFile = new FileStream(tempFilePath, FileMode.Create);
await file.CopyToAsync(tempFile);
await Share.Default.RequestAsync(new ShareFileRequest
{
Title = "Save database backup",
File = new ShareFile(tempFilePath)
});
#else
try
{
await FileSaver.Default.SaveAsync("basemix.sqlite3", file, cts.Token);
Expand All @@ -110,6 +119,7 @@
// Potential bug in CommunityToolkit for android. Seems to be an issue with returning the filepath
// and not with actually saving the file. I can safely ignore it for now.
}
#endif
}
catch (Exception e)
{
Expand Down
3 changes: 0 additions & 3 deletions src/Basemix/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

0 comments on commit 3e38d8b

Please sign in to comment.