0

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();
        }


    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Big T Larrity
  • 221
  • 2
  • 9
  • The short answer is that there is no need to instantiate a `File` object - https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.8 . As such, this is a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . **Why** do you think you need to do that? – mjwills Jun 17 '19 at 03:46
  • @mjwills thanks for looking. The reason is , I have a string of my 'Music' directory in my phone. But I want to iterate through all, find all of type mp3 and then add them to a list, which I can cycle through in the StartMediaPlayer(filetype) method – Big T Larrity Jun 17 '19 at 03:48
  • well, did you see the answer in the attached link? it has like 250 likes and is accepted answer. It seemed to work for them for a similar problem (I;m VERY NEW to Android but have some experience using Unity for games dev) – Big T Larrity Jun 17 '19 at 03:49
  • 1
    @BigTLarrity you understand that the accepted answer is written in `Java`, right? – vasily.sib Jun 17 '19 at 03:50
  • Ooooooh! i see! We'll damn :[. Thanks for the links in the closed duplicate statement though. Perhaps this line is all i need? "string[] music = Directory.GetFiles(dir, "*.mp3");" – Big T Larrity Jun 17 '19 at 03:52
  • just trying now, but i realise, the music is in subfolders so will probably have a problem – Big T Larrity Jun 17 '19 at 03:53
  • Read the docs for https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.8#System_IO_Directory_GetFiles_System_String_System_String_System_IO_SearchOption_ . – mjwills Jun 17 '19 at 03:55

0 Answers0