我希望能够预先配置我的Android应用程序以包含可读/可写的json文件,该文件也可在其他平台上使用(iOS,UWP)。我开始查看AssetManager但发现我只能读取文件,而不是稍后编辑它。
内部存储是答案吗?如果是这样,我如何使用此文件预先填充应用程序的内部存储?除了在C#代码中执行完整的写入方法之外,我在网上找不到任何关于如何执行此操作的文档,这种方法在所有平台上都无法共享相同的config.json文件。我只是希望我的Android应用程序存储此config.json文件,并使该文件可读写。
任何帮助将不胜感激。
我决定回答我自己的问题并展示如何在代码中完成。在这个答案中,我使用Android资产和内部存储来操作包含我的序列化对象的文件。
1)将您的文件复制到Xamarin.Android项目的Assets文件夹中,并将其Build Action更改为“AndroidAsset”。这将确保该文件最初与您的Android应用程序打包在一起。
2)在您的C#代码中,您真正想要的是从Android App的内部存储器读取和写入此文件,而不是从AssetManager读取(无论如何都不能写入AssetManager,只能读取!)。因此,要执行此操作,必须首先从AssetManager中提取文件,然后所有后续读取/写入都将从内部存储中进行。
/// <summary>
/// This method will read the application settings from internal storage.
/// If it can't find it, it will use the default AndroidAsset version of
/// the appsettings.json file from this project's Asset folder.
/// </summary>
/// <returns></returns>
public async Task<DefaultSettings> GetDefaultSettingsAsync()
{
var path = Application.Context.FilesDir.Path;
if (File.Exists(Path.Combine(path, "appsettings.json")))
{
using (var sr = Application.Context.OpenFileInput("appsettings.json"))
{
using (var ms = new MemoryStream())
{
await sr.CopyToAsync(ms);
var memoryBytes = ms.ToArray();
var content = System.Text.Encoding.Default.GetString(memoryBytes);
var defaultSettings = JsonConvert.DeserializeObject<DefaultSettings>(content);
return defaultSettings;
}
}
}
else
{
var result = await GetDefaultSettingsAssetAsync();
return result;
}
}
/// <summary>
/// This method gets the appsettings.json file from the Android AssetManager, in the case
/// where you do not have an editable appsettings.json file yet in your Android app's internal storage.
/// </summary>
/// <returns></returns>
private static async Task<DefaultSettings> GetDefaultSettingsAssetAsync()
{
try
{
var assetsManager = MainActivity.Instance.Assets;
using (var sr = assetsManager.Open("appsettings.json"))
{
using (var ms = new MemoryStream())
{
await sr.CopyToAsync(ms);
var memoryBytes = ms.ToArray();
var content = System.Text.Encoding.Default.GetString(memoryBytes);
var defaultSettings = JsonConvert.DeserializeObject<DefaultSettings>(content);
// This will write the Asset to Internal Storage for the first time. All subsequent writes will occur using SaveDefaultSettingsAsync.
using (var stream = Application.Context.OpenFileOutput("appsettings.json", FileCreationMode.Private))
{
await stream.WriteAsync(memoryBytes, 0, memoryBytes.Length);
}
return defaultSettings;
}
}
}
catch (Exception ex)
{
return new DefaultSettings();
}
}
/// <summary>
/// This method will save the DefaultSettings to your appsettings.json file in your Android App's internal storage.
/// </summary>
/// <param name="settings"></param>
/// <returns></returns>
public async Task SaveDefaultSettingsAsync(DefaultSettings settings)
{
using (var stream = Application.Context.OpenFileOutput("appsettings.json", FileCreationMode.Private))
{
var content = JsonConvert.SerializeObject(settings);
var bytes = Encoding.Default.GetBytes(content);
await stream.WriteAsync(bytes, 0, bytes.Length);
}
}