0

enter image description hereI am trying to write a set of bytes converted from an mp3 file back to an mp3 file to temporarily store in a users mobile device but cannot seem to get the right folder path of where to write to.

I have tried using Directory.GetCurrentDirectory(), Environment.CurrentDirectory, and other variations of these to get the current directory but am only getting back null or just one /, so I'm not sure if I am using these right for this scenario. The only method of getting any path I have found to work is FileSystem.CacheDirectory which gives /data/user/0/com.companyname.lal/cache/.

I think if I am just temporarily wanting to store this mp3 file and then delete it soon after it plays this is maybe the right idea using the cache for both Android and iOS. It appears to work, however I cannot find this /data/user/0/com.companyname.lal/cache/ full path of where my android emulator files are located. There is a data folder but no user or any of those other subfolders.

Like I said I am ultimately wanting to just write and store this file temporarily on a user's mobile device that I can delete it soon after playing, but for now I need to just figure out how to write the file at all where it can be accessed in code.

  • See following : https://stackoverflow.com/questions/2364740/where-is-the-temp-folder-on-android-devices – jdweng Feb 26 '23 at 17:32
  • 1
    have you read the [docs](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data)? Xamarin includes a `SpecialFolder` enum that provides appropriate storage locations for user files – Jason Feb 26 '23 at 18:16
  • @jdweng I do not have that `/data/local` folder and @Jason I cannot get any filepath using that method. I am wanting to get the stream using `GetManifestResourceStream` so I may actually need to store the mp3 file in a namespace folder in order to properly access and read it – milkman33995 Feb 27 '23 at 20:04
  • The extension must match the type of media in the ASCII header. So make sure you are saving the file with an extension. The temp folder should be the one specified in my link above. Also check the bye count of the original file against what you receive to make sure you didn't loose any bytes. Then make sure you are using the same player manually as in the code. You may need to change the player that is used for the extension. It is always a good idea to set the File Explorer to show file extensions. Never understood why Microsoft default is no extension in file explorer. – jdweng Feb 27 '23 at 20:24
  • @jdweng Trying that folder path `"/data/local/tmp/0.mp3"` gives me an error. I am still guessing that it may not exist. The file was an mp3 before converting to bytes and that is the file extension I am using as well. Would I be able to accomplish just deleting it after storing and playing it from a namespace folder using `GetManifestResourceStream`? – milkman33995 Feb 27 '23 at 20:43
  • Try any folder that works and make sure you didn't loose any bytes. – jdweng Feb 27 '23 at 20:53
  • That is the issue, I cannot get any pathname besides using `FileSystem.CacheDirectory`, but I cannot play the mp3 file storing it there. Even trying to just manually set the path to `@"C:/Users/myUser/dev/myProject"` it cannot find. I have set all the folder permissions to read and write access except the Users folder I cannot seem to change that main folder permissions and I cannot store the file outside of the Users folder so I am not sure of what else to do here. – milkman33995 Feb 27 '23 at 23:05

1 Answers1

1

As Jason said, you can use the Environment.SpecialFolder and Environment.GetFolderPath methods to select a suitable location for storage and recall, the following code stores the file in the System directory:

File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.System),test);

If the System directory returns empty, you can use ApplicationData to get the path of the appdate. I created a Xamarin.Forms project and tested it on both iOS and Android simulators, and the path can be returned successfully.

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

enter image description here

For more information on the use of methods, you can refer to the official documentation: Environment.SpecialFolder Enum

Environment.GetFolderPath Method

Update: If you need to read a file, for example if you use WriteAllBytes:

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "xxx");
File.WriteAllBytes(fileName, XXX);
var test=File.ReadAllBytes(fileName);

For more details, please refer to: File Handling in Xamarin.Forms

Zack
  • 1,255
  • 1
  • 2
  • 5
  • Using `var path = Environment.GetFolderPath(Environment.SpecialFolder.System);` returns an empty string `""`. Does that method work for Xamarin? – milkman33995 Feb 27 '23 at 20:02
  • @milkman33995 I updated my answer, you can change to `ApplicationData` to try it out. – Zack Feb 28 '23 at 01:47
  • Okay so using that `ApplicationData` I did get a path `/data/user/0/com.companyname.lal/files/.config/0.mp3` so I do think this is the place I will need to store it. But I still cannot find this folder, when going to the folder location of my emulator the `data` folder only has empty subfolders and no `user` folder. Unless I am looking at the wrong emulator folder, I do not see that path anywhere. I have also uploaded a photo showing you the folder. – milkman33995 Feb 28 '23 at 02:26
  • In Xamarin, the ApplicationData directory is the application's internal storage and is not exposed to the user. This directory stores application data and configuration files, and you can normally read and write in this directory. – Zack Feb 28 '23 at 02:41
  • So how would I read an mp3 file from this file location? `GetManifestResourceStream` requires the file to be accessible from a namespace folder inside the project so if that filepath is not being exposed inside the namespace how can I read this mp3 file? The [docs](https://learn.microsoft.com/en-us/xamarin/essentials/file-system-helpers?context=xamarin%2Fandroid&tabs=android) have an example for reading text files but nothing else. – milkman33995 Feb 28 '23 at 18:26
  • @milkman33995 I updated my answer, you can try it. – Zack Mar 01 '23 at 01:45