Xamarin 和 android 媒体和照片权限 - 应用程序在发布模式下崩溃
Xamarin and android Permissions for Media and Photos - app crashes in release mode
我有一个很奇怪的问题,我认为它与权限有关。当我 运行 我的应用程序处于调试模式时,它 运行 没问题,但是当我 运行 它处于发布模式时它崩溃了。我认为它可能与权限有关的原因是当我 运行 应用程序处于调试模式时,当它部署在我的测试设备(我的三星 phone)上时,它要求我允许访问照片和媒体。我已经在清单中设置了所有权限,甚至与照片和媒体远程相关,并且在 MainActivity.cs
中有代码来检查这些权限,但是当我 运行 在我的 phone 上调试应用程序时它仍然要求获得访问照片和媒体的权限。我在清单中没有看到任何特别提到照片的内容,就像我之前所说的那样,我在清单中授予了对甚至提到“媒体”的所有内容的访问权限。我的应用程序还访问互联网、前台和其他需要设置权限的区域,我已经在清单中设置了这些并且没有问题,只有这个。我在这里错过了什么?
我的MainActivity.cs
代码:
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ForegroundService) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ForegroundService }, 1);
}
if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.Internet) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Internet }, 1);
}
if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.AccessNetworkState) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessNetworkState }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.AccessMediaLocation) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessMediaLocation }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.MediaContentControl) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.MediaContentControl }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Camera }, 1);
}
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.logon);
RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
UserDialogs.Init(this);
Button MyButton_Submit = FindViewById<Android.Widget.Button>(Resource.Id.button_submit);
aTextboxUsername = FindViewById<EditText>(Resource.Id.aTextboxUsername);
aTextboxPassword = FindViewById<EditText>(Resource.Id.aTextboxPassword);
InputMethodManager board = (InputMethodManager)GetSystemService(Context.InputMethodService);
board.HideSoftInputFromWindow(aTextboxUsername.WindowToken, 0);
Button MyButton_CreateAccount = FindViewById<Android.Widget.Button>(Resource.Id.button_createaccount);
MyButton_CreateAccount.SetBackgroundColor(Android.Graphics.Color.YellowGreen);
MyButton_Submit.Click += async (sender, e) =>
{
ManualResetEvent resetEvent = new ManualResetEvent(false);
ProgressBarHandler myprogressbar = new ProgressBarHandler(this);
myprogressbar.show();
var thread = new System.Threading.Thread(new ThreadStart(async delegate
{
await SignOn(myprogressbar);
resetEvent.Set();
}));
thread.Start();
await Task.Run(() => resetEvent.WaitOne());
while (thread.ThreadState == ThreadState.Running)
{
await Task.Delay(100);
}
myprogressbar.hide();
if (myprogressbar.ErrorMesage != null)
{
showMessage(myprogressbar.ErrorMesage);
}
};
MyButton_CreateAccount.Click += (sender, e) =>
{
Intent intent = new Intent(this, typeof(CreateAccount));
Bundle bundlee = new Bundle();
intent.PutExtra("TheBundle", bundlee);
StartActivity(intent);
};
}
catch (Exception ex)
{
Utils.LogError("An error occured in MainActivity.cs, the error is: " + ex.Message);
}
}
*** 更新 ***
这是我在设备日志中看到的:
Time Device Name Type PID Tag Message
06-24 08:58:00.417 Samsung SM-G973U Warning 1141 ContextImpl Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1162 com.android.server.am.AppErrors.crashApplicationInner:579 com.android.server.am.AppErrors.crashApplication:443 com.android.server.am.ActivityManagerService.handleApplicationCrashInner:12323 com.android.server.am.ActivityManagerService.handleApplicationCrash:12258
Force finishing activity My.App/crc64a23c41873b6fbff5.MainActivity
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate skip_space_check : 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate skeymaster_only: 0, keystore_only: 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate shutdown_broadcast: 0, shutdown_delay: 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate sec_mode : 1 dump_flags : 247031436, booting_delay : 0 booting_enospc : 0 sys_rescue : 0 sys_error : 0 sys_native : 0 sys_watchdog : 0 app_error : 1 app_native : 0 app_anr : 0
06-24 08:58:00.416 Samsung SM-G973U Info 2468 dumpstate do_zip_file: 1 do_vibrate: 0 use_socket: 0 use_control_socket: 0 do_screenshot: 0 is_remote_mode: 0 show_header_only: 0 do_start_service: 0 telephony_only: 0 wifi_only: 0 do_progress_updates: 0 fd: 10 bugreport_mode: BUGREPORT_A_ERROR dumpstate_hal_mode: DEFAULT limited_only: 0 args:
06-24 08:58:00.416 Samsung SM-G973U Warning 1141 ActivityManager crash : My.App,10511
好吧,经过 2 天的痛苦过程后,我(显然)弄明白了。
我在项目->属性->Android选项中关闭(未选中)Enable Multi-Dex
该应用现在可以在发布模式下正常运行。
我有一个很奇怪的问题,我认为它与权限有关。当我 运行 我的应用程序处于调试模式时,它 运行 没问题,但是当我 运行 它处于发布模式时它崩溃了。我认为它可能与权限有关的原因是当我 运行 应用程序处于调试模式时,当它部署在我的测试设备(我的三星 phone)上时,它要求我允许访问照片和媒体。我已经在清单中设置了所有权限,甚至与照片和媒体远程相关,并且在 MainActivity.cs
中有代码来检查这些权限,但是当我 运行 在我的 phone 上调试应用程序时它仍然要求获得访问照片和媒体的权限。我在清单中没有看到任何特别提到照片的内容,就像我之前所说的那样,我在清单中授予了对甚至提到“媒体”的所有内容的访问权限。我的应用程序还访问互联网、前台和其他需要设置权限的区域,我已经在清单中设置了这些并且没有问题,只有这个。我在这里错过了什么?
我的MainActivity.cs
代码:
protected override void OnCreate(Bundle savedInstanceState)
{
try
{
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ForegroundService) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ForegroundService }, 1);
}
if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.Internet) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Internet }, 1);
}
if (ActivityCompat.CheckSelfPermission(this,Android.Manifest.Permission.AccessNetworkState) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessNetworkState }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.AccessMediaLocation) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessMediaLocation }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.MediaContentControl) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.MediaContentControl }, 1);
}
if (ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Camera }, 1);
}
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.logon);
RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
UserDialogs.Init(this);
Button MyButton_Submit = FindViewById<Android.Widget.Button>(Resource.Id.button_submit);
aTextboxUsername = FindViewById<EditText>(Resource.Id.aTextboxUsername);
aTextboxPassword = FindViewById<EditText>(Resource.Id.aTextboxPassword);
InputMethodManager board = (InputMethodManager)GetSystemService(Context.InputMethodService);
board.HideSoftInputFromWindow(aTextboxUsername.WindowToken, 0);
Button MyButton_CreateAccount = FindViewById<Android.Widget.Button>(Resource.Id.button_createaccount);
MyButton_CreateAccount.SetBackgroundColor(Android.Graphics.Color.YellowGreen);
MyButton_Submit.Click += async (sender, e) =>
{
ManualResetEvent resetEvent = new ManualResetEvent(false);
ProgressBarHandler myprogressbar = new ProgressBarHandler(this);
myprogressbar.show();
var thread = new System.Threading.Thread(new ThreadStart(async delegate
{
await SignOn(myprogressbar);
resetEvent.Set();
}));
thread.Start();
await Task.Run(() => resetEvent.WaitOne());
while (thread.ThreadState == ThreadState.Running)
{
await Task.Delay(100);
}
myprogressbar.hide();
if (myprogressbar.ErrorMesage != null)
{
showMessage(myprogressbar.ErrorMesage);
}
};
MyButton_CreateAccount.Click += (sender, e) =>
{
Intent intent = new Intent(this, typeof(CreateAccount));
Bundle bundlee = new Bundle();
intent.PutExtra("TheBundle", bundlee);
StartActivity(intent);
};
}
catch (Exception ex)
{
Utils.LogError("An error occured in MainActivity.cs, the error is: " + ex.Message);
}
}
*** 更新 ***
这是我在设备日志中看到的:
Time Device Name Type PID Tag Message
06-24 08:58:00.417 Samsung SM-G973U Warning 1141 ContextImpl Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1162 com.android.server.am.AppErrors.crashApplicationInner:579 com.android.server.am.AppErrors.crashApplication:443 com.android.server.am.ActivityManagerService.handleApplicationCrashInner:12323 com.android.server.am.ActivityManagerService.handleApplicationCrash:12258
Force finishing activity My.App/crc64a23c41873b6fbff5.MainActivity
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate skip_space_check : 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate skeymaster_only: 0, keystore_only: 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate shutdown_broadcast: 0, shutdown_delay: 0
06-24 08:58:00.417 Samsung SM-G973U Info 2468 dumpstate sec_mode : 1 dump_flags : 247031436, booting_delay : 0 booting_enospc : 0 sys_rescue : 0 sys_error : 0 sys_native : 0 sys_watchdog : 0 app_error : 1 app_native : 0 app_anr : 0
06-24 08:58:00.416 Samsung SM-G973U Info 2468 dumpstate do_zip_file: 1 do_vibrate: 0 use_socket: 0 use_control_socket: 0 do_screenshot: 0 is_remote_mode: 0 show_header_only: 0 do_start_service: 0 telephony_only: 0 wifi_only: 0 do_progress_updates: 0 fd: 10 bugreport_mode: BUGREPORT_A_ERROR dumpstate_hal_mode: DEFAULT limited_only: 0 args:
06-24 08:58:00.416 Samsung SM-G973U Warning 1141 ActivityManager crash : My.App,10511
好吧,经过 2 天的痛苦过程后,我(显然)弄明白了。
我在项目->属性->Android选项中关闭(未选中)Enable Multi-Dex
该应用现在可以在发布模式下正常运行。