My application requires access to external storage, so I need to check if WRITE_EXTERNAL is granted, and if not prompt the user, set the permission and then continue the app *** the app cannot continue without out this, as I need to build a data file with SQLite in a specific folder that I create. Problem is the code I am using (see below) does not block the app from continuing, so the app goes on ahead and tries to access the SQLite file/folder which is not created yet until the user "ALLOWS" access - Question is how can I make sure the user clicks "Allow" before continuing on?
thanks
JK
---- CODE - this is Main Activity
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
MainActivityHook = this;
CheckAppPermissions(); // *********************** this is the call to get the permission and the create directory call
if(!Directory.Exists("/mnt/sdcard/mycustomfolder/"))
Directory.CreateDirectory("/mnt/sdcard/mycustomfolder/");
LoadApplication(new App());
}
const int PERMISSIONS_STORAGE = 111;
public void CheckAppPermissions()
{
if(ContextCompat.CheckSelfPermission(MainActivity.MainActivityHook, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
{
ActivityCompat.RequestPermissions(MainActivity.MainActivityHook, new System.String[] { Manifest.Permission.WriteExternalStorage }, PERMISSIONS_STORAGE);
}
}
public override void OnRequestPermissionsResult(int requestCode,
string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
switch(requestCode)
{
case PERMISSIONS_STORAGE:
{
if(grantResults.Length > 0 && grantResults[0] == Permission.Granted)
{
}
else
{
Toast.MakeText(ApplicationContext, "Application cannot continue without access to the local storage device. Exiting...", ToastLength.Long).Show();
JavaSystem.Exit(0);
}
break;
}
}
}
Answers
@jamesklett Use following link for permission.
https://github.com/jamesmontemagno/PermissionsPlugin
I don't use plugins - I write my own code - but thanks for the tip