protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var bundle = savedInstanceState ?? this.Intent.Extras; var isComplete = bundle.GetBoolean("isComplete", false); this.requestId = bundle.GetInt(IntentId, 0); this.action = bundle.GetString(IntentAction); this.type = bundle.GetString(IntentType); var allowMultiple = bundle.GetBoolean(Intent.ExtraAllowMultiple, false); Intent intent = null; try { intent = new Intent(this.action); intent.SetType(this.type); if (allowMultiple) { intent.PutExtra(Intent.ExtraAllowMultiple, true); } if (!isComplete) { this.StartActivityForResult(intent, this.requestId); } } catch (Exception ex) { #if DEBUG System.Diagnostics.Debug.WriteLine(ex.ToString()); #endif FilesReceived?.Invoke(this, new FileOpenPickerFilesReceived(this.requestId, null, true)); } finally { intent?.Dispose(); } }
/// <summary> /// OnCreate /// </summary> /// <param name="savedInstanceState"></param> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var b = (savedInstanceState ?? Intent.Extras); var ran = b.GetBoolean("ran", defaultValue: false); title = b.GetString(MediaStore.MediaColumns.Title); description = b.GetString(MediaStore.Images.ImageColumns.Description); tasked = b.GetBoolean(ExtraTasked); id = b.GetInt(ExtraId, 0); type = b.GetString(ExtraType); front = b.GetInt(ExtraFront); if (type == "image/*") { isPhoto = true; } action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(action); if (action == Intent.ActionPick) { pickIntent.SetType(type); pickIntent.PutExtra(Intent.ExtraAllowMultiple, b.GetBoolean(ExtraMultiple)); } else { if (!isPhoto) { seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, seconds); } size = b.GetLong(MediaStore.ExtraSizeLimit, 0); if (size != 0) { pickIntent.PutExtra(MediaStore.ExtraSizeLimit, size); } } saveToAlbum = b.GetBoolean(ExtraSaveToAlbum); pickIntent.PutExtra(ExtraSaveToAlbum, saveToAlbum); quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(quality)); if (front != 0) { pickIntent.PutExtra(ExtraFront, (int)Android.Hardware.CameraFacing.Front); } if (!ran) { path = GetOutputMediaFile(this, b.GetString(ExtraPath), title, isPhoto, false); Touch(); if (path.Scheme == "file") { try { var photoURI = FileProvider.GetUriForFile(this, Application.Context.PackageName + ".fileprovider", new Java.IO.File(path.Path)); GrantUriPermissionsForIntent(pickIntent, photoURI); pickIntent.AddFlags(ActivityFlags.GrantReadUriPermission); pickIntent.AddFlags(ActivityFlags.GrantWriteUriPermission); pickIntent.PutExtra(MediaStore.ExtraOutput, photoURI); } catch (Java.Lang.IllegalArgumentException iae) { //Using a Huawei device on pre-N. Increased likelihood of failure... if (huaweiManufacturer.Equals(Build.Manufacturer, StringComparison.CurrentCultureIgnoreCase) && (int)Build.VERSION.SdkInt < 24) { pickIntent.PutExtra(MediaStore.ExtraOutput, path); } else { System.Diagnostics.Debug.WriteLine($"Unable to get file location, check and set manifest with file provider. Exception: {iae}"); throw new ArgumentException("Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.", iae); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to get file location, check and set manifest with file provider. Exception: {ex}"); throw new ArgumentException("Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.", ex); } } else { pickIntent.PutExtra(MediaStore.ExtraOutput, path); } } else { path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { StartActivityForResult(pickIntent, id); } } catch (Exception ex) { OnMediaPicked(new MediaPickedEventArgs(id, ex)); //must finish here because an exception has occured else blank screen Finish(); } finally { pickIntent?.Dispose(); } }
/// <summary> /// OnCreate /// </summary> /// <param name="savedInstanceState"></param> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bundle b = (savedInstanceState ?? Intent.Extras); bool ran = b.GetBoolean("ran", defaultValue: false); this.title = b.GetString(MediaStore.MediaColumns.Title); this.description = b.GetString(MediaStore.Images.ImageColumns.Description); this.tasked = b.GetBoolean(ExtraTasked); this.id = b.GetInt(ExtraId, 0); this.type = b.GetString(ExtraType); this.front = b.GetInt(ExtraFront); if (this.type == "image/*") { this.isPhoto = true; } this.action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(this.action); if (this.action == Intent.ActionPick) { pickIntent.SetType(type); } else { if (!this.isPhoto) { this.seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (this.seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, seconds); } } this.saveToAlbum = b.GetBoolean(ExtraSaveToAlbum); pickIntent.PutExtra(ExtraSaveToAlbum, this.saveToAlbum); this.quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(this.quality)); if (front != 0) { pickIntent.PutExtra(ExtraFront, (int)Android.Hardware.CameraFacing.Front); } if (!ran) { this.path = GetOutputMediaFile(this, b.GetString(ExtraPath), this.title, this.isPhoto, false); Touch(); var targetsNOrNewer = false; try { targetsNOrNewer = (int)Application.Context.ApplicationInfo.TargetSdkVersion >= 24; } catch (Exception appInfoEx) { System.Diagnostics.Debug.WriteLine("Unable to get application info for targetSDK, trying to get from package manager: " + appInfoEx); targetsNOrNewer = false; var appInfo = PackageManager.GetApplicationInfo(Application.Context.PackageName, 0); if (appInfo != null) { targetsNOrNewer = (int)appInfo.TargetSdkVersion >= 24; } } if (targetsNOrNewer && this.path.Scheme == "file") { var photoURI = FileProvider.GetUriForFile(this, Application.Context.PackageName + ".fileprovider", new Java.IO.File(this.path.Path)); pickIntent.PutExtra(MediaStore.ExtraOutput, photoURI); } else { pickIntent.PutExtra(MediaStore.ExtraOutput, this.path); } } else { this.path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { StartActivityForResult(pickIntent, this.id); } } catch (Exception ex) { OnMediaPicked(new MediaPickedEventArgs(this.id, ex)); //must finish here because an exception has occured else blank screen Finish(); } finally { if (pickIntent != null) { pickIntent.Dispose(); } } }
/// <summary> /// OnCreate /// </summary> /// <param name="savedInstanceState"></param> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bundle b = (savedInstanceState ?? Intent.Extras); bool ran = b.GetBoolean("ran", defaultValue: false); this.title = b.GetString(MediaStore.MediaColumns.Title); this.description = b.GetString(MediaStore.Images.ImageColumns.Description); this.tasked = b.GetBoolean(ExtraTasked); this.id = b.GetInt(ExtraId, 0); this.type = b.GetString(ExtraType); if (this.type == "image/*") { this.isPhoto = true; } this.action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(this.action); if (this.action == Intent.ActionPick) { pickIntent.SetType(type); } else { if (!this.isPhoto) { this.seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (this.seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, seconds); } } this.quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(this.quality)); if (!ran) { this.path = GetOutputMediaFile(this, b.GetString(ExtraPath), this.title, this.isPhoto); Touch(); pickIntent.PutExtra(MediaStore.ExtraOutput, this.path); } else { this.path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { StartActivityForResult(pickIntent, this.id); } } catch (Exception ex) { OnMediaPicked(new MediaPickedEventArgs(this.id, ex)); } finally { if (pickIntent != null) { pickIntent.Dispose(); } } }
/// <summary> /// OnCreate /// </summary> /// <param name="savedInstanceState"></param> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var b = savedInstanceState ?? Intent.Extras; var ran = b.GetBoolean("ran", false); _Title = b.GetString(MediaStore.MediaColumns.Title); _Description = b.GetString(MediaStore.Images.ImageColumns.Description); _Tasked = b.GetBoolean(ExtraTasked); _Id = b.GetInt(ExtraId, 0); _Type = b.GetString(ExtraType); _Front = b.GetInt(ExtraFront); if (_Type == "image/*") { _IsPhoto = true; } _Action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(_Action); if (_Action == Intent.ActionPick) { pickIntent.SetType(_Type); } else { if (!_IsPhoto) { _Seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (_Seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, _Seconds); } } _SaveToAlbum = b.GetBoolean(ExtraSaveToAlbum); pickIntent.PutExtra(ExtraSaveToAlbum, _SaveToAlbum); _Quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(_Quality)); if (_Front != 0) { pickIntent.PutExtra(ExtraFront, (int)Android.Hardware.CameraFacing.Front); } if (!ran) { _Path = GetOutputMediaFile(this, b.GetString(ExtraPath), _Title, _IsPhoto, false); Touch(); bool targetsNOrNewer; try { targetsNOrNewer = (int)app.Application.Context.ApplicationInfo.TargetSdkVersion >= 24; } catch (Exception appInfoEx) { System.Diagnostics.Debug.WriteLine("Unable to get application info for targetSDK, trying to get from package manager: " + appInfoEx); targetsNOrNewer = false; var appInfo = PackageManager.GetApplicationInfo(app.Application.Context.PackageName, 0); if (appInfo != null) { targetsNOrNewer = (int)appInfo.TargetSdkVersion >= 24; } } if (targetsNOrNewer && _Path.Scheme == "file") { throw new NotImplementedException(); } pickIntent.PutExtra(MediaStore.ExtraOutput, _Path); } else { _Path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { StartActivityForResult(pickIntent, _Id); } } catch (Exception ex) { OnMediaPicked(new MediaPickedEventArgs(_Id, ex)); //must finish here because an exception has occured else blank screen Finish(); } finally { pickIntent?.Dispose(); } }
/// <summary> /// Called when the activity is starting. /// </summary> /// <param name="savedInstanceState">If the activity is being re-initialized after /// previously being shut down then this Bundle contains the data it most /// recently supplied in <c><see cref="M:Android.App.Activity.OnSaveInstanceState(Android.OS.Bundle)" /></c>. <format type="text/html"><b><i>Note: Otherwise it is null.</i></b></format></param> /// <since version="Added in API level 1" /> /// <altmember cref="M:Android.App.Activity.OnStart" /> /// <altmember cref="M:Android.App.Activity.OnSaveInstanceState(Android.OS.Bundle)" /> /// <altmember cref="M:Android.App.Activity.OnRestoreInstanceState(Android.OS.Bundle)" /> /// <altmember cref="M:Android.App.Activity.OnPostCreate(Android.OS.Bundle)" /> /// <remarks><para tool="javadoc-to-mdoc">Called when the activity is starting. This is where most initialization /// should go: calling <c><see cref="M:Android.App.Activity.SetContentView(System.Int32)" /></c> to inflate the /// activity's UI, using <c><see cref="M:Android.App.Activity.FindViewById(System.Int32)" /></c> to programmatically interact /// with widgets in the UI, calling /// <c><see cref="M:Android.App.Activity.ManagedQuery(Android.Net.Uri, System.String[], System.String[], System.String[], System.String[])" /></c> to retrieve /// cursors for data being displayed, etc. /// </para> /// <para tool="javadoc-to-mdoc">You can call <c><see cref="M:Android.App.Activity.Finish" /></c> from within this function, in /// which case onDestroy() will be immediately called without any of the rest /// of the activity lifecycle (<c><see cref="M:Android.App.Activity.OnStart" /></c>, <c><see cref="M:Android.App.Activity.OnResume" /></c>, /// <c><see cref="M:Android.App.Activity.OnPause" /></c>, etc) executing. /// </para> /// <para tool="javadoc-to-mdoc"> /// <i>Derived classes must call through to the super class's /// implementation of this method. If they do not, an exception will be /// thrown.</i> /// </para> /// <para tool="javadoc-to-mdoc"> /// <format type="text/html"> /// <a href="http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)" target="_blank">[Android Documentation]</a> /// </format> /// </para></remarks> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var b = (savedInstanceState ?? Intent.Extras); var ran = b.GetBoolean("ran", false); _title = b.GetString(MediaStore.MediaColumns.Title); _description = b.GetString(MediaStore.Images.ImageColumns.Description); _tasked = b.GetBoolean(ExtraTasked); _id = b.GetInt(ExtraId, 0); _type = b.GetString(ExtraType); if (_type == "image/*") { _isPhoto = true; } _action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(_action); if (_action == Intent.ActionPick) { pickIntent.SetType(_type); } else { if (!_isPhoto) { _seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (_seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, _seconds); } } _quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(_quality)); if (!ran) { _path = GetOutputMediaFile(this, b.GetString(ExtraPath), _title, _isPhoto); Touch(); pickIntent.PutExtra(MediaStore.ExtraOutput, _path); } else { _path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { if (global::Android.OS.Build.VERSION.Release == "6.0") { if (CheckSelfPermission(Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted) { RequestPermissions(new string[] { Manifest.Permission.Camera }, 1); } } StartActivityForResult(pickIntent, _id); } } catch (Exception ex) { RaiseOnMediaPicked(new MediaPickedEventArgs(_id, ex)); } finally { if (pickIntent != null) { pickIntent.Dispose(); } } }
/// <summary> /// Called when the activity is starting. /// </summary> /// <param name="savedInstanceState">If the activity is being re-initialized after /// previously being shut down then this Bundle contains the data it most /// recently supplied in <c><see cref="M:Android.App.Activity.OnSaveInstanceState(Android.OS.Bundle)" /></c>. <format type="text/html"><b><i>Note: Otherwise it is null.</i></b></format></param> /// <since version="Added in API level 1" /> /// <altmember cref="M:Android.App.Activity.OnStart" /> /// <altmember cref="M:Android.App.Activity.OnSaveInstanceState(Android.OS.Bundle)" /> /// <altmember cref="M:Android.App.Activity.OnRestoreInstanceState(Android.OS.Bundle)" /> /// <altmember cref="M:Android.App.Activity.OnPostCreate(Android.OS.Bundle)" /> /// <remarks><para tool="javadoc-to-mdoc">Called when the activity is starting. This is where most initialization /// should go: calling <c><see cref="M:Android.App.Activity.SetContentView(System.Int32)" /></c> to inflate the /// activity's UI, using <c><see cref="M:Android.App.Activity.FindViewById(System.Int32)" /></c> to programmatically interact /// with widgets in the UI, calling /// <c><see cref="M:Android.App.Activity.ManagedQuery(Android.Net.Uri, System.String[], System.String[], System.String[], System.String[])" /></c> to retrieve /// cursors for data being displayed, etc. /// </para> /// <para tool="javadoc-to-mdoc">You can call <c><see cref="M:Android.App.Activity.Finish" /></c> from within this function, in /// which case onDestroy() will be immediately called without any of the rest /// of the activity lifecycle (<c><see cref="M:Android.App.Activity.OnStart" /></c>, <c><see cref="M:Android.App.Activity.OnResume" /></c>, /// <c><see cref="M:Android.App.Activity.OnPause" /></c>, etc) executing. /// </para> /// <para tool="javadoc-to-mdoc"> /// <i>Derived classes must call through to the super class's /// implementation of this method. If they do not, an exception will be /// thrown.</i> /// </para> /// <para tool="javadoc-to-mdoc"> /// <format type="text/html"> /// <a href="http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)" target="_blank">[Android Documentation]</a> /// </format> /// </para></remarks> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var b = (savedInstanceState ?? Intent.Extras); var ran = b.GetBoolean("ran", false); _title = b.GetString(MediaStore.MediaColumns.Title); _description = b.GetString(MediaStore.Images.ImageColumns.Description); _tasked = b.GetBoolean(EXTRA_TASKED); _id = b.GetInt(EXTRA_ID, 0); _type = b.GetString(EXTRA_TYPE); if (_type == "image/*") { _isPhoto = true; } _action = b.GetString(EXTRA_ACTION); Intent pickIntent = null; try { pickIntent = new Intent(_action); if (_action == Intent.ActionPick) { pickIntent.SetType(_type); } else { if (!_isPhoto) { _seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (_seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, _seconds); } } _quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(_quality)); if (!ran) { _path = GetOutputMediaFile(this, b.GetString(EXTRA_PATH), _title, _isPhoto); Touch(); pickIntent.PutExtra(MediaStore.ExtraOutput, _path); } else { _path = Uri.Parse(b.GetString(EXTRA_PATH)); } } if (!ran) { StartActivityForResult(pickIntent, _id); } } catch (Exception ex) { RaiseOnMediaPicked(new MediaPickedEventArgs(_id, ex)); } finally { if (pickIntent != null) { pickIntent.Dispose(); } } }
/// <summary> /// OnCreate /// </summary> /// <param name="savedInstanceState"></param> protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var b = (savedInstanceState ?? Intent.Extras); var ran = b.GetBoolean("ran", defaultValue: false); title = b.GetString(MediaStore.MediaColumns.Title); description = b.GetString(MediaStore.Images.ImageColumns.Description); tasked = b.GetBoolean(ExtraTasked); id = b.GetInt(ExtraId, 0); type = b.GetString(ExtraType); front = b.GetInt(ExtraFront); if (type == "image/*") { isPhoto = true; } action = b.GetString(ExtraAction); Intent pickIntent = null; try { pickIntent = new Intent(action); if (action == Intent.ActionPick) { pickIntent.SetType(type); } else { if (!isPhoto) { seconds = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (seconds != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, seconds); } size = b.GetLong(MediaStore.ExtraSizeLimit, 0); if (size != 0) { pickIntent.PutExtra(MediaStore.ExtraSizeLimit, size); } } saveToAlbum = b.GetBoolean(ExtraSaveToAlbum); pickIntent.PutExtra(ExtraSaveToAlbum, saveToAlbum); quality = (VideoQuality)b.GetInt(MediaStore.ExtraVideoQuality, (int)VideoQuality.High); pickIntent.PutExtra(MediaStore.ExtraVideoQuality, GetVideoQuality(quality)); if (front != 0) { pickIntent.UseFrontCamera(); } else { pickIntent.UseBackCamera(); } if (!ran) { path = GetOutputMediaFile(this, b.GetString(ExtraPath), title, isPhoto, false); Touch(); var targetsNOrNewer = false; try { targetsNOrNewer = (int)Application.Context.ApplicationInfo.TargetSdkVersion >= 24; } catch (Exception appInfoEx) { System.Diagnostics.Debug.WriteLine("Unable to get application info for targetSDK, trying to get from package manager: " + appInfoEx); targetsNOrNewer = false; var appInfo = PackageManager.GetApplicationInfo(Application.Context.PackageName, 0); if (appInfo != null) { targetsNOrNewer = (int)appInfo.TargetSdkVersion >= 24; } } if (targetsNOrNewer && path.Scheme == "file") { try { var photoURI = FileProvider.GetUriForFile(this, Application.Context.PackageName + ".fileprovider", new Java.IO.File(path.Path)); GrantUriPermissionsForIntent(pickIntent, photoURI); pickIntent.AddFlags(ActivityFlags.GrantReadUriPermission); pickIntent.AddFlags(ActivityFlags.GrantWriteUriPermission); pickIntent.PutExtra(MediaStore.ExtraOutput, photoURI); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to get file location, check and set manifest with file provider. Exception: {ex}"); throw new ArgumentException("Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.", ex); } } else { pickIntent.PutExtra(MediaStore.ExtraOutput, path); } } else { path = Uri.Parse(b.GetString(ExtraPath)); } } if (!ran) { StartActivityForResult(pickIntent, id); } } catch (Exception ex) { OnMediaPicked(new MediaPickedEventArgs(id, ex)); //must finish here because an exception has occured else blank screen Finish(); } finally { if (pickIntent != null) { pickIntent.Dispose(); } } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _context = Application.Context; bool isPhoto = Intent.GetBooleanExtra(ExtraIsPhoto, false); Intent intent = new Intent(isPhoto ? MediaStore.ActionImageCapture : MediaStore.ActionVideoCapture); try { if (!isPhoto) { var isPixel = false; try { var name = Settings.System.GetString(_context.ContentResolver, "device_name"); isPixel = name.Contains("Pixel") || name.Contains("pixel"); } catch (Exception) { } int seconds = Intent.GetIntExtra(MediaStore.ExtraDurationLimit, 0); if (seconds != 0 && !isPixel) { intent.PutExtra(MediaStore.ExtraDurationLimit, seconds); } long size = Intent.GetLongExtra(MediaStore.ExtraSizeLimit, 0); if (size != 0) { intent.PutExtra(MediaStore.ExtraSizeLimit, size); } } var quality = Intent.GetIntExtra(MediaStore.ExtraVideoQuality, 1); intent.PutExtra(MediaStore.ExtraVideoQuality, quality); // 启用前/后摄像头 if (Intent.GetBooleanExtra(ExtraFront, false)) { intent.UseFrontCamera(); } else { intent.UseBackCamera(); } // 照片或视频保存的文件 _path = System.IO.Path.Combine(Kit.CachePath, Kit.NewGuid + (isPhoto ? ".jpg" : ".mp4")); var file = new Java.IO.File(_path); file.CreateNewFile(); try { // 将访问受限的 file:// URI 转化为可以授权共享的 content:// URI // 已将Kit.CachePath共享,配置见:AndroidManifest.xml 和 Resources\xml\file_path.xml var uri = FileProvider.GetUriForFile(this, _context.PackageName + ".fileprovider", file); GrantUriPermissionsForIntent(intent, uri); intent.AddFlags(ActivityFlags.GrantReadUriPermission); intent.AddFlags(ActivityFlags.GrantWriteUriPermission); intent.PutExtra(MediaStore.ExtraOutput, uri); } catch (Java.Lang.IllegalArgumentException iae) { // Using a Huawei device on pre-N. Increased likelihood of failure... if (_huaweiManufacturer.Equals(Build.Manufacturer, StringComparison.CurrentCultureIgnoreCase) && (int)Build.VERSION.SdkInt < 24) { intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file)); } else { System.Diagnostics.Debug.WriteLine($"Unable to get file location, check and set manifest with file provider. Exception: {iae}"); throw new ArgumentException("Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.", iae); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to get file location, check and set manifest with file provider. Exception: {ex}"); throw new ArgumentException("Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.", ex); } StartActivityForResult(intent, 1337); } catch (Exception) { OnCaptured(null); //must finish here because an exception has occured else blank screen Finish(); } finally { if (intent != null) { intent.Dispose(); } } }
/// <summary> /// Called when the activity is starting. /// </summary> /// <param name="savedInstanceState"> /// If the activity is being re-initialized after previously being shut down then this Bundle contains /// the data it most recently supplied in <see cref="OnSaveInstanceState"/>. Otherwise it is <c>null</c>. /// </param> protected override void OnCreate(Bundle savedInstanceState) { // Call the base member base.OnCreate(savedInstanceState); // Take the state from the saved instance or an intent var b = savedInstanceState ?? this.Intent.Extras; var ran = b.GetBoolean("ran", false); this.title = b.GetString(MediaStore.MediaColumns.Title); this.description = b.GetString(MediaStore.Images.ImageColumns.Description); this.tasked = b.GetBoolean(MediaPickerActivity.ExtraTasked); this.id = b.GetInt(MediaPickerActivity.ExtraId, 0); this.type = b.GetString(MediaPickerActivity.ExtraType); // Parse media type if (this.type == "image/*") { this.isPhoto = true; } // Get the requested action this.action = b.GetString(MediaPickerActivity.ExtraAction); Intent pickIntent = null; try { // Create the media pick intent pickIntent = new Intent(this.action); if (this.action == Intent.ActionPick) { pickIntent.SetType(this.type); } else { // Setup the video properties for video pickIntent.PutExtra(MediaStore.ExtraVideoQuality, 1); if (!this.isPhoto) { this.durationLimit = b.GetInt(MediaStore.ExtraDurationLimit, 0); if (this.durationLimit != 0) { pickIntent.PutExtra(MediaStore.ExtraDurationLimit, this.durationLimit); } } // If the activity was not run if (!ran) { this.path = MediaPickerActivity.GetOutputMediaFile( this, b.GetString(MediaPickerActivity.ExtraPath), this.title, this.isPhoto); this.Touch(); pickIntent.PutExtra(MediaStore.ExtraOutput, this.path); } else { // Get the media location this.path = Uri.Parse(b.GetString(MediaPickerActivity.ExtraPath)); } } // If the activity was not run before for the current operation if (!ran) { // Check camera permission if (global::Android.OS.Build.VERSION.Release == "6.0") { if (this.CheckSelfPermission(Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted) { this.RequestPermissions(new[] { Manifest.Permission.Camera }, 1); } } // Start the media operation this.StartActivityForResult(pickIntent, this.id); } } catch (Exception ex) { // Handle exception MediaPickerActivity.RaiseOnMediaPicked(new MediaPickedEventArgs(this.id, ex)); } finally { // Cleanup pickIntent?.Dispose(); } }