Home > other >  Xamarin access to external public folder (documents/download)
Xamarin access to external public folder (documents/download)

Time:11-25

i was using GetExternalStoragePublicDirectory but is deprecated now, it gets back the standard path "/storage/emulated/0/Download" but from Android 12 i can save, load only the file stored from my app, if i add the same file renamed by pc is "filtered" and not accessible, it looks like not present! i store a txt file from my app, how can i work around to access again to download external public folder?

CodePudding user response:

I have create a sample to test the GetExternalStoragePublicDirectory method and create a txt file in the Download folder. Here is the MainActivity's code:

            Button write = this.FindViewById<Button>(Resource.Id.buttonwrite);
            Button read = this.FindViewById<Button>(Resource.Id.buttonread);
            write.Click  = (s, e) =>
            {
                var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
                var file = Path.Combine(path   "/app.txt");
                using (System.IO.FileStream os = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate))
                {
                    string temp = "this is test string";
                    byte[] buffer = Encoding.UTF8.GetBytes(temp);
                    os.Write(buffer, 0, buffer.Length);
                }
                Intent intent = new Intent(DownloadManager.ActionViewDownloads);
                StartActivity(intent);
                // this line code can open the download folder and view all the files in it. When the user click the file, it will be open
            };
            read.Click  = (s, e) =>
            {
                var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
                var file = Path.Combine(path   "/app.txt");
           /*     using (System.IO.FileStream os = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate))
                {
                    StreamReader streamReader = new StreamReader(os);
                    var content = streamReader.ReadToEnd();
                } */
                Intent intent = new Intent(Intent.ActionOpenDocument);
                intent.AddCategory(Intent.CategoryOpenable);
                intent.SetType("text/*");
                intent.PutExtra(DocumentsContract.ExtraInitialUri, Uri.Parse(file));
                StartActivityForResult(intent, 2);
                //This code can open the file picker and let the user to choose a file
            };

The app.txt file can still be read when I renamed as New.txt by the file manager. Just change var file = Path.Combine(path "/app.txt"); to var file = Path.Combine(path "/New.txt"); and use the code in the /* can read it. So if the file was created by your app, your app can access it even though the file has been renamed by the uesr. But you can access the file in the public folder which was not created by your app.

In addidion, if you don't have to push your app to the google play, you can use the access all file permission according to my old answer in this link and the answer about the deprecated GetExternalStoragePublicDirectory.

CodePudding user response:

Thanks, and what about the file lists in folder? i'll have to push my app to google play, i read that if i'm using GetExternalStoragePublicDirectory can be rejected, so i have to use MediaStore to access to external public folder.

this is the method to write a file into download folder and works. now i'm writing the read file, but i have to get back the file folder. ios is more simple :)

  • Related