Xamarin.android, открытие намерения селектора изображения из фрагмента диалога
Вы можете использовать StartActivityForResult () в вашем DialogFragment
, чтобы реализовать эту функцию.
Чтобы сделать снимок с камеры:
Intent takePicture = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(takePicture, 0);//zero can be replaced with any action code
Чтобы выбрать фотографию из галереи:
Intent pickPhoto = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
StartActivityForResult(pickPhoto, 1);
В вашем Activity
переопределите OnActivityResult()
:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 0:
if (resultCode == Result.Ok)
{
Android.Net.Uri selectedImage = data.Data;
}
break;
case 1:
if (resultCode == Result.Ok)
{
Android.Net.Uri selectedImage = data.Data;
}
break;
}
}
Обновление:
Мой DialogFragment
:
public class MyDialogFragment : DialogFragment//Android.Support.V4.App.DialogFragment
{
public static MyDialogFragment NewInstance(Bundle bundle)
{
MyDialogFragment fragment = new MyDialogFragment();
fragment.Arguments = bundle;
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.fragment_dialog, container, false);
Button button = view.FindViewById<Button>(Resource.Id.dismiss_dialog_button);
button.Click += delegate {
Dismiss();
Toast.MakeText(Activity, "Dialog fragment dismissed!", ToastLength.Short).Show();
};
Button dialog_button = view.FindViewById<Button>(Resource.Id.dialog_button);
dialog_button.Click += delegate {
Intent takePicture = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(takePicture, 0);//zero can be replaced with any action code
//Intent pickPhoto = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
//StartActivityForResult(pickPhoto, 1);
};
return view;
}
}
Мой Activity
:
public class MainActivity : AppCompatActivity
{
...
public void ShowDialog()
{
var ft = SupportFragmentManager.BeginTransaction();
//Remove fragment else it will crash as it is already added to backstack
Android.Support.V4.App.Fragment prev = SupportFragmentManager.FindFragmentByTag("dialog");
if (prev != null)
{
ft.Remove(prev);
}
ft.AddToBackStack(null);
// Create and show the dialog.
MyDialogFragment newFragment = MyDialogFragment.NewInstance(null);
//Add fragment
newFragment.Show(ft, "dialog");
}
...
}