I have been searching for two days continuously on this problem, but I did not get any answer, I want to create a file on the storage in Android and put data in it. but always there is an error on it . error: Access to path "/storage/emulated/0/esi2.txt" is denied.
list of permissions:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.ehsanbest" android:installLocation="auto">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true"></application>
</manifest>
public class MainActivity : AppCompatActivity
{
Button _start;
Button _stop;
TextView _text1;
TextView _text2;
TextView _text3;
MediaRecorder Recorder;
static readonly int WRITE_EXTERNAL_STORAGE = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
_start = FindViewById<Button>(Resource.Id.materialButton1);
_stop = FindViewById<Button>(Resource.Id.materialButton2);
_text1 = FindViewById<TextView>(Resource.Id.textView1);
_text2 = FindViewById<TextView>(Resource.Id.textView2);
_text3 = FindViewById<TextView>(Resource.Id.textView3);
string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath "/esi2.txt";
_start.Click = (sender, e) =>
{
_stop.Enabled = !_stop.Enabled;
_start.Enabled = !_start.Enabled;
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new String[] { Android.Manifest.Permission.Camera }, WRITE_EXTERNAL_STORAGE);
}
else
{
ActivityCompat.RequestPermissions(this, new String[] { Android.Manifest.Permission.WriteExternalStorage }, 1);
}
_text2.Text = path.ToString();
eCreateFile(path);
};
}
public void eCreateFile(string FileName)
{
string text = "Ehsan Test";
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
try
{
File.WriteAllBytes(FileName, data);
}
catch(Exception ex)
{
_text3.Text = ex.Message;
}
}
probably I think function ActivityCompat.RequestPermissions(this, ) does nothing. Nothing happens when running it. While waiting for the permission window to open. but Nothing happens and Access is denied.
I installed the plugin Plugin.Permissions from nuget but nothing changes.
I did everything in answers in this stack overflow question:How to request permissions in Xamarin.Android?
but nothing
CodePudding user response:
In Xamarin.Android File Access / Reading or Writing to files on internal storage, code snippet shows how to get path with correct folder for your app:
var filePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "esi2.txt");