question

shhamidi-2358 avatar image
0 Votes"
shhamidi-2358 asked WenyanZhang-MSFT commented

how can set file to hidden and show

I will create a folder to download the audio file, but I do not want the user to be able to access those files on their mobile phone. I just want it to be played through the app

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i get my path from xamarin android and use it xamarin forms
i want to hidden folder and use files from it

0 Votes 0 ·

May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT edited

Hello,

Welcome to our Microsoft Q&A platform!

There are two ways to do ,using Internal Storage or setting the file hidden.

Internal Storage– this is a portion of the file system that can be accessed only by the application or the operating system.
I save a text file, you could refer to the following code

 protected override void OnAppearing()
         {
             base.OnAppearing();
             String str = "123456";//test
             Byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);
             string fileName = "test.txt";
             string documentPath = FileSystem.CacheDirectory;
             string path = Path.Combine(documentPath, fileName);
             File.WriteAllBytes(path, byteArray);
         }

Create a hidden folder simply by adding a '.' to the front of the folder name.
I save the test.text file by External Storage , you could reder to the following code


  private void Button_Clicked(object sender, EventArgs e)
             {
                 String str = "123456";//test
                 Byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);
                 string fileName = "test.txt";
                 String testPath = DependencyService.Get<PathService>().GetPath();
                 var folderPath = testPath + "/.test";
                 if (!Directory.Exists(folderPath))//creat a folder to save
                 {
                     DirectoryInfo dirInfor = new DirectoryInfo(folderPath);
                     dirInfor.Create();
                 }
                 var filePath = System.IO.Path.Combine(folderPath, fileName);
                 File.WriteAllBytes(filePath, byteArray);
             }

use DependencyService to get path

 public interface PathService
     {
         String GetPath();
     }


Android

 [assembly: Dependency(typeof(PathServiceAndroid))]
 namespace AndroidStrogeDemo.Droid
 {
     public class PathServiceAndroid : PathService
     {
         public string GetPath()
         {
             string testPath = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;
             return testPath;
         }
     }
 }

permission

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

However, you want to save video, internal storage directories tend to be small , your app should query the free space on the device before writing app-specific files to internal storage. If you create a hidden folder, user still can set "Don't show hidden files" or "Show hidden files". There is not a perfect way to do this, because this is the feature of Android.

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.