@Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) I think that renaming the extension would be easiest, I can make the app use .db or .sqlite form here on out, and add do some fancy footwork to migrate older database files that need to be renamed.
I have set the proper MIME type "application/vnd.sqlite3", and I have tried renaming the file to both .db, and .sqlite. It seems that no matter what, I can see the file in the picker, but it's greyed out and I cannot select the file?
I am trying to make this code reusable so I created this wrapper class that I call elsewhere in my code to find the file. This class is a bit ugly at the moment, but from what I can tell it should be working as expected. I've stepped through it line by line and it appears to be setting the MIME type and executing the options appropriately.
class ImportFileDialog
{
/// <summary>
/// private storage Options
/// </summary>
private PickOptions options;
/// <summary>
/// Gets or sets the value for Options
/// </summary>
public PickOptions Options
{
get => options;
set
{
options = value;
}
}
/// <summary>
/// private storage FileTypes
/// </summary>
private FilePickerFileType fileTypes;
/// <summary>
/// Gets or sets the value for FileTypes
/// </summary>
public FilePickerFileType FileTypes
{
get => fileTypes;
set
{
fileTypes = value;
}
}
/// <summary>
/// private storage ResultingFile
/// </summary>
private FileResult resultingFile;
/// <summary>
/// Gets or sets the value for ResultingFile
/// </summary>
public FileResult ResultingFile
{
get => resultingFile;
set
{
resultingFile = value;
}
}
/// <summary>
/// private storage Title
/// </summary>
private String title;
/// <summary>
/// Gets or sets the value for Title
/// </summary>
public String Title
{
get => title ?? string.Empty;
set
{
title = value;
}
}
public ImportFileDialog(String incoming)
{
Title = incoming;
InitClass();
}
public ImportFileDialog()
{
InitClass();
}
private void InitClass()
{
Options = new PickOptions();
Title = "Select File";
}
public void SetDBFile()
{
FileTypes = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
// { DevicePlatform.iOS, new[] { ".db3" } },
{ DevicePlatform.Android, new[] { "application/vnd.sqlite3" } },
// { DevicePlatform.UWP, new[] { ".db3" } },
});
}
public void SetXMLFile()
{
FileTypes = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { ".xml" } },
{ DevicePlatform.Android, new[] { ".xml" } },
{ DevicePlatform.UWP, new[] { ".xml" } },
});
}
public async Task<FileResult> getFile()
{
Options.FileTypes = FileTypes;
Options.PickerTitle = Title;
ResultingFile = await Xamarin.Essentials.FilePicker.PickAsync(Options);
if (ResultingFile == null)
{
return null;
}
else
{
return ResultingFile;
}
}
}
And my code that consumes this class is this .
ImportFileDialog ifd = new ImportFileDialog();
ifd.Title = "Import Database";
ifd.SetDBFile();
var incomingFile = await ifd.getFile();
I'll try some more testing in depth tomorrow, but wanted to see if there were something simple I am missing.
Thanks again!