Context.GetExternalFilesDir() 不接受 null
Context.GetExternalFilesDir() is not accepting null
我正在尝试获取我的 c# xamarin android 应用程序的私有外部存储目录的路径。微软文档告诉我们使用
Android.Content.Context.GetExternalFilesDir(string)
然而,当我尝试使用这个函数时,我收到关于该函数的以下错误:
An object reference is required for the non-static field, method or property
这是我的代码
class RecorderController {
string externalStoragePath;
public RecorderController() {
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
// I have also tried the following:
// string s = null;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
// Even when I try to get the path to the Downloads folder, I get the same error:
// string s = Android.OS.Environment.DirectoryDownloads;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
}
}
我不知道如何解决这个问题,有人知道我做错了什么吗?
尝试改变
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
至
externalStoragePath = Path.Combine(GetExternalFilesDir(null).AbsolutePath, "recordings");
显然我必须使用 Context 的实例,就像 Mike Christensen 评论的那样。我还错过了 GetExternalFilesDir returns 一个 File 对象,我应该在它之后使用 AbsolutePath,就像 Leo Zhu 说的那样。
我将代码更改为
class RecorderController {
string externalStoragePath;
public RecorderController(Context con) {
PermanenteOpslagPad = Path.Combine(con.GetExternalFilesDir(null).AbsolutePath, "recordings");
}
}
我正在尝试获取我的 c# xamarin android 应用程序的私有外部存储目录的路径。微软文档告诉我们使用
Android.Content.Context.GetExternalFilesDir(string)
然而,当我尝试使用这个函数时,我收到关于该函数的以下错误:
An object reference is required for the non-static field, method or property
这是我的代码
class RecorderController {
string externalStoragePath;
public RecorderController() {
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
// I have also tried the following:
// string s = null;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
// Even when I try to get the path to the Downloads folder, I get the same error:
// string s = Android.OS.Environment.DirectoryDownloads;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
}
}
我不知道如何解决这个问题,有人知道我做错了什么吗?
尝试改变
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
至
externalStoragePath = Path.Combine(GetExternalFilesDir(null).AbsolutePath, "recordings");
显然我必须使用 Context 的实例,就像 Mike Christensen 评论的那样。我还错过了 GetExternalFilesDir returns 一个 File 对象,我应该在它之后使用 AbsolutePath,就像 Leo Zhu 说的那样。
我将代码更改为
class RecorderController {
string externalStoragePath;
public RecorderController(Context con) {
PermanenteOpslagPad = Path.Combine(con.GetExternalFilesDir(null).AbsolutePath, "recordings");
}
}