I am trying to access/create a list of all files in a directory for an Android app in C#.
I find these answers to help me: How to list files in an android directory? (specifically those by Yuri and Reed)
In the answers they have line such as File directory = new File("xyz")
.
But when I try the same, I get error "Cannot declare a variable of static type 'File'".
I have tried to search for that problem, but all I can find is techniques of how to call static methods from within 'non-static??' classes/methods.
My code is as follows: (comments where the error is)
namespace Music_Player_v001
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected MediaPlayer mediaPlayer;
List<string> filepaths_audio = new List<string>();
public void StartMediaPlayer(String filePath)
{
mediaPlayer.Reset();
mediaPlayer.SetDataSource(filePath);
mediaPlayer.Prepare();
mediaPlayer.Start();
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
string musicDirectoryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).ToString();
Log.Info("FILEPATHS: ", "music directory path is: " + musicDirectoryPath);
mediaPlayer = new MediaPlayer();
StartMediaPlayer(musicDirectoryPath + "/NewPipe/Intergalactic - Beastie Boys (HD).m4a"); // Test with file I know is there, it Works!
File directory = new File(musicDirectoryPath); /// I GET ERROR HERE <//--------------------
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
return true;
}
return base.OnOptionsItemSelected(item);
}
private void FabOnClick(object sender, EventArgs eventArgs)
{
View view = (View) sender;
Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
.SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
}
}
}