Xamarin.android,从对话框片段中打开图像选择器意图

时间:2018-11-18 21:25:49

标签: android xamarin dialog fragment

我的xamarin android应用程序中有一个对话框片段,用户可以在其中填写表格。在此表单中,有一个选择图像的按钮,但是我能找到的唯一选择图像的方法是使用带有“ * image”选项集的意图。 问题是,当我单击按钮时,在我关闭对话框片段之前,意图不会打开,而这不是解决方案。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  

Xamarin.android,从对话框片段中打开图像选择器意图

您可以在DialogFragment中使用StartActivityForResult()来实现此功能。

要从相机拍照:

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");
    }

    ...
}