首页 > 解决方案 > Xamarin.android,从对话框片段打开图像选择器意图

问题描述

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

有任何想法吗?

标签: androidxamarindialogfragment

解决方案


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

    ...
}

推荐阅读