MvvmCross FilePlugin 保存文件到SD卡
MvvmCross FilePlugin save files to SD card
我们在我们的应用程序中使用漂亮的 MvvmCross 框架,我们还利用 FilePlugin 以跨平台的方式处理文件系统。
默认情况下,FilePlugin 将数据存储在某些默认位置,例如 /data/data/<appname>
中的 Android。
但是,如果我想存储大文件,如视频或 3D 模型,该怎么办?在 iOS 中,您将所有内容都存储在应用程序文件夹中,但在 Android 中,您可能希望将文件存储在 SD 卡上。
对于这个用例,您会推荐什么解决方案?
我的意思是,我是否应该继承 FilePlugin 并以某种方式覆盖 Android 的应用程序根目录?
我遇到了同样的问题,并通过创建我自己的 IMvxFileStore 实现解决了这个问题。我的实现允许我指定一个绝对路径。
internal class CustomMvxAndroidFileStore : MvxFileStore
{
private Context _context;
private Context Context
{
get
{
if (_context == null)
{
_context = Mvx.Resolve<IMvxAndroidGlobals>().ApplicationContext;
}
return _context;
}
}
protected override string FullPath(string path)
{
if (PathIsAbsolute(path)) return path;
return Path.Combine(Context.FilesDir.Path, path);
}
private bool PathIsAbsolute(string path)
{
return path.StartsWith("/");
}
}
然后在应用安装过程中注入:
Mvx.RegisterType<IMvxFileStore, CustomMvxAndroidFileStore>();
这对我来说很好。
我们在我们的应用程序中使用漂亮的 MvvmCross 框架,我们还利用 FilePlugin 以跨平台的方式处理文件系统。
默认情况下,FilePlugin 将数据存储在某些默认位置,例如 /data/data/<appname>
中的 Android。
但是,如果我想存储大文件,如视频或 3D 模型,该怎么办?在 iOS 中,您将所有内容都存储在应用程序文件夹中,但在 Android 中,您可能希望将文件存储在 SD 卡上。
对于这个用例,您会推荐什么解决方案? 我的意思是,我是否应该继承 FilePlugin 并以某种方式覆盖 Android 的应用程序根目录?
我遇到了同样的问题,并通过创建我自己的 IMvxFileStore 实现解决了这个问题。我的实现允许我指定一个绝对路径。
internal class CustomMvxAndroidFileStore : MvxFileStore
{
private Context _context;
private Context Context
{
get
{
if (_context == null)
{
_context = Mvx.Resolve<IMvxAndroidGlobals>().ApplicationContext;
}
return _context;
}
}
protected override string FullPath(string path)
{
if (PathIsAbsolute(path)) return path;
return Path.Combine(Context.FilesDir.Path, path);
}
private bool PathIsAbsolute(string path)
{
return path.StartsWith("/");
}
}
然后在应用安装过程中注入:
Mvx.RegisterType<IMvxFileStore, CustomMvxAndroidFileStore>();
这对我来说很好。