[英]How to capture an image in xamarin android using the camera in fragment
I am a beginner in xamarin android.I want to take photo using my device camera,I am using the following code to to capture the image but, I got an exception when running the code 我是xamarin android的初学者。我想使用设备相机拍照,我正在使用以下代码来捕获图像,但是,运行代码时出现异常
Here is my Fragment.CS 这是我的Fragment.CS
public class ScanFragments : Fragment
{
ImageView imageView;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static ScanFragments NewInstance()
{
var scan = new ScanFragments { Arguments = new Bundle() };
return scan;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
imageView =View.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =View. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
return inflater.Inflate(Resource.Layout.ScanLayout, null);
}
private void BtnCamera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
}
Well now I see your mistake its actually very simple you get the null reference because of the following reasons 好吧,现在我看到您的错误实际上非常简单,由于以下原因,您获得了空引用
1.OnCreateView does not contain a definition of View basically what I mean here is View is a class which you are using as an object of that class. 1.OnCreateView基本上不包含View的定义,我的意思是View是一个用作该类对象的类。
2.The actual way I recommend using fragments is first you define it an XML or an AXML as its foreground view in on OnCreateView as follows : 2.我建议使用片段的实际方法是,首先在OnCreateView上将其定义为XML或AXML作为其前景视图,如下所示:
return inflater.Inflate(Resource.Layout.ScanLayout, null);
3.Override the OnViewCreated method and use its View to do all your work as follows : 3.重写OnViewCreated方法并使用其View来完成所有工作,如下所示:
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
imageView =view.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =view. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
}
4.For details related to the Android Activity and Fragment lifecycle check here 4,有关Android活动和片段生命周期的详细信息,请点击此处
Goodluck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.