This is my first Custom Renderer project and just getting hold of xamarin. I have a Camera Custom Renderer, and want to pass the image preview (through the file path) to another activity for extra functions, in Android. Using the classic:
Intent intent = new Intent(this, typeof(ResultPage));
StartActivity(intent);
It throws to errors: -The first one in "this", says that "error CS1503 argument 1: 'CustomRenderer.Droid.CameraPageRenderer' canto be converted to 'Android.Content.Context'" -Second, is in "StartActivity" that says "Error CS0103 The name 'StartActivity' doesn't exist in the actual context 'CustomRenderer.Android'"
Here's is the method where it should go:
async void TakePhotoButtonTapped(object sender, EventArgs e)
{
camera.StopPreview();
var image = textureView.Bitmap;
try
{
var absolutePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
var folderPath = absolutePath + "/Images";
var filePath = System.IO.Path.Combine(folderPath, string.Format("image_{0}.jpg", Guid.NewGuid()));
var fileStream = new FileStream(filePath, FileMode.Create);
await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 50, fileStream);
fileStream.Close();
image.Recycle();
var intent = new Android.Content.Intent(Android.Content.Intent.ActionMediaScannerScanFile);
var file = new Java.IO.File(filePath);
var uri = Android.Net.Uri.FromFile(file);
intent.SetData(uri);
MainActivity.Instance.SendBroadcast(intent);
CameraPageRenderer cameraPageRenderer = this;
Intent intent = new Intent(this, typeof(ResultPage));
StartActivity(intent);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(@" ", ex.Message);
}
A CustomRenderer
is not an Android Context
. You need to retrieve the current Android Context
and use it:
var context = this.Context;
Intent intent = new Intent(context, typeof(ResultPage));
context.StartActivity(intent);
Remember that your custom renderer needs to implement the parameterized constructor that accepts a Context as it's parameter in order for this to work:
public MyCustomRenderer(Context context) : base(context) { }