Home>
/current situation

In xamarin.Android, I cannot edit a file that has been copied (copied after connecting to a Windows PC) to an external storage (for example,/storage/emulated/0/Documents /).
(Copy/read from PC is possible)
I want to output (write) a file from the application

I tried to create a process to get permission and write, but it is not written to the file (reading is possible)
I connected to Windows 10 via USB and looked at the files in "PC \ HUAWEI MediaPad M3 Lite 10 \ Internal Storage \ Documents", but determined that they were not updated.

Is there something fundamentally wrong?
Is it just an environmental issue?
If anyone knows anything, please let me know

using System;
using System.IO;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.Content;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
namespace RcvWriteTest1
{
    [Activity (Label = "@ string/app_name", Theme = "@ style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity: AppCompatActivity
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            Xamarin.Essentials.Platform.Init (this, savedInstanceState);
            SetContentView (Resource.Layout.activity_main);
            Android.Support.V7.Widget.Toolbar toolbar = FindViewById  (Resource.Id.toolbar);
            SetSupportActionBar (toolbar);
            Button btnEnd = (Button) FindViewById (Resource.Id.button2);
            btnEnd.Click + = BtnEnd_Click;
            Button btnWrite = (Button) FindViewById (Resource.Id.button1);
            btnWrite.Click + = BtnWrite_Click;
        }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 ();
        }
        private void BtnEnd_Click (object sender, EventArgs e)
        {
            this.FinishAndRemoveTask ();// Exit the application
            // Forcibly terminate the application
            System.Diagnostics.Process.GetCurrentProcess (). CloseMainWindow ();
        }
        private void BtnWrite_Click (object sender, EventArgs e)
        {
            // Confirm write permission
            var permissioncheckW = ContextCompat.CheckSelfPermission (this, Manifest.Permission.WriteExternalStorage);
            // Request if you do not have write permission
            if (permissioncheckW == Android.Content.PM.Permission.Denied)
            {
                ActivityCompat.RequestPermissions (this, new []
                {
                    Manifest.Permission.WriteExternalStorage
                },

 0);
            }
            string strbase = Android.OS.Environment.ExternalStorageDirectory + "/ Documents /";string path = Path.Combine (strbase, "TEST.txt");
            string path2 = Path.Combine (strbase, "TEST.CSV");
            // FileInfo file = new FileInfo (path);
            var Lines = System.IO.File.ReadAllLines (path2, Encoding.GetEncoding ("shift-jis"));
            // ***** I try to write here, but nothing is written in TEST.txt ... *****
            try
            {
                System.IO.File.WriteAllText (path, "TEST \ n");
            }
            catch (Exception ex)
            {
                Toast.MakeText (this, ex.Message, ToastLength.Short) .Show ();
            }
        }
        public override void OnRequestPermissionsResult (int requestCode, string [] permissions, [GeneratedEnum] Android.Content.PM.Permission [] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult (requestCode, permissions, grantResults);
            // Display a dialog when requested and all permissions are granted
            if (grantResults.Cast<Permission>(). All (x =>x == Permission.Granted))
            {
                new Android.App.AlertDialog.Builder (this)
                               .SetTitle ("Runtime Permission")
                               .SetMessage ("All Granted")
                               .SetPositiveButton ("OK", (_, __) =>{})
                               .Show ();
            }
            base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
        }
    }
}


When I tried the file writing process in the above process, it seems that no error has occurred, but writing is not possible (even though it does not come to the catch side).
(It's okay to use another folder, so I just need to be able to input and output the files I added ...)

What I tried

-File reading (System.IO.File.ReadLines) No problem
-File writing (System.IO.File.WriteAllText) Cannot write when confirmed from Window
-If i read the same file after writing, the written contents can be read.
-Actually, I can write, but is the recognition different on Android and Windows 10?

environment

android: Android7.0 (HUAWEI MediaPad M3 Lite 10)
PC: Windows10, Visualstudio2019 (xamarin)

  • Answer # 1

    I'm not sure exactly how "I can't edit" can't be done, but a media scan (or restart) is required to recognize the file created on Android on the PC.
    Please restart your smartphone and open the Documents folder to see it.
    When the files created so far are displayed, please perform a media scan on the created files.