About file generation when the application is started for the first time

木幡 弘文 1 Reputation point
2021-10-26T07:07:54.997+00:00

When the app is started for the first time, folders and files are created, but the app crashes due to an error.
I have file permissions, what should I do?

[AndroidManifest.xml] Permission is set and
At the first startup, the permission confirmation screen is displayed.
After that, [Start.cs] is executed.

//AndroidManifest.xml-------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0.0" package="jp.XXXXXX.XXXX" android:installLocation="preferExternal" android:versionCode="000000">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
    <application android:label="XXXXXXXX" android:networkSecurityConfig="@xml/network_security_config" android:hardwareAccelerated="false" android:requestLegacyExternalStorage="true" android:largeHeap="true" android:supportsRtl="true" android:usesCleartextTraffic="true" android:icon="@drawable/splash_logo">
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyB34hG3jxWQBog6MMjkaUEK43ZKrtXtWOM" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <uses-library android:name="org.apache.http.legacy" android:required="false" />
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
//-------------

//Start.cs--------------------------
public Start()
{

    try
    {

        private string FolderPath = "XXXXXXXX";

        InitializeComponent();

        if (Directory.Exists(FolderPath) == false)
        {
            Directory.CreateDirectory(FolderPath);
        }

     }
     catch
     {

     }
}
//--------------------------
Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-10-27T04:31:43.617+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Do you get System.UnauthorizedAccessException?

    If so, you did not grant the runtime write external permission. So you cannot create a directory without permission(your permission confirmation screen pop up, but you did not grand the permssion at this time).

    So, please open your MainActivity.cs, find the OnRequestPermissionsResult method. using following code to check the write permission if granded. If granted, we can execute the start.

       public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
               {   
                   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);   
         
                   int WRITE_EXTERNAL_STORAGE=0;   
                   if (requestCode == WRITE_EXTERNAL_STORAGE)   
                   {   
         
                       for (int i = 0; i < permissions.Length; i++)   
                       {   
                           if (permissions[i] == "android.permission.WRITE_EXTERNAL_STORAGE" && grantResults[i] == Permission.Granted)   
                           {   
                               Start();   
                           }   
                           else   
                           {   
                               Log.Info("TAG", "WRITE_EXTERNAL_STORAGE  permission was NOT granted.");   
                           }   
                       }   
                          
         
                         
                   }   
                   else   
                   {   
                       base.OnRequestPermissionsResult(requestCode, permissions, grantResults);   
                   }   
                      
               }  
     
    

    Best Regards,

    Leon Lu


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.