【问题标题】:Xamarin.android, opening the image selector intent from dialog fragmentXamarin.android,从对话框片段打开图像选择器意图
【发布时间】:2018-11-18 21:25:49
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 我已经测试了我的代码,它在我这边运行良好,请检查一下。 :)
  • 您解决了这个问题吗?
  • 您解决了这个问题吗?

标签: android xamarin dialog fragment


【解决方案1】:

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

    ...
}

【讨论】:

  • @RasmusSpaceflunkJørgensen,你的DialogFragmentActivity是什么类型的?
  • @RasmusSpaceflunkJørgensen, Android.Support.V4.App.DialogFragmentAndroid.App.DialogFragment?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多