android - Xamarin.android,从对话框片段打开图像选择器意图
问题描述
我的 xamarin android 应用程序中有一个对话框片段,用户可以在其中填写表格。在这种形式中,有一个选择图像的按钮,但我能找到的唯一选择图像的方法是使用带有“*image”选项集的意图。问题是当我单击按钮时,在我关闭对话框片段之前,意图不会打开,这不是解决方案。
有任何想法吗?
解决方案
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");
}
...
}
推荐阅读
- php - 有没有更好的方法在 php 中为 ajax 返回 .json 数组?
- c - GCC:函数参数中的静态数组索引不会触发任何警告
- git - git 添加 . 导致功能未实现错误
- python - matplotlib PolygonSelector 在 tkinter 中调用时冻结
- java - 加载大位图时如何处理 OutOfMemoryError 而不损失质量?
- javascript - 如何按顺序动态运行 Promise
- python - 基于标志的 Python DataFrame 累加器
- python - 如何在 Python 中获得 R 的 ccf?
- ruby-on-rails - Active Model Serializer 不使用 JSON:API 适配器
- scala - 符号'术语
类路径中缺少 .shapeless'。这个符号是 'value blink.linalg.operators.MatrixGenericOps.v1ne' 所必需的