how to access camera on older versions (kitkat ,lollipop) in xamarin android

Dev_MaranDev_Maran USMember ✭✭
edited May 2017 in Xamarin.Android

I am working on xamarin android app in which i have to implemented Camera app using guide(https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/
) provided by xamarin documents and working on marshmallow.

Code that i am using :

if (IsThereAnAppToTakePictures())
{
CreateDirectoryForPictures();
cameraIcon.Click += TakeAPicture;
}
private void CreateDirectoryForPictures()
{
_dir = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "kudzu");
if (!_dir.Exists())
{
_dir.Mkdirs();
}
}

private bool IsThereAnAppToTakePictures()
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
IList availableActivities = this.Activity.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}
private void TakeAPicture(object sender, EventArgs eventArgs)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
file = new File(_dir, string.Format("Image{0}.jpg", Guid.NewGuid())); //_file and _dir is instance variables
intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
StartActivityForResult(intent, REQUEST_CAMERA);
}

public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult (requestCode, resultCode, data);

// Make it available in the gallery

Intent mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile);
Uri contentUri = Uri.FromFile (_file);
mediaScanIntent.SetData (contentUri);
SendBroadcast (mediaScanIntent);

// Display in ImageView. We will resize the bitmap to fit the display.
// Loading the full sized image will consume to much memory
// and cause the application to crash.

int height = Resources.DisplayMetrics.HeightPixels;
int width = _imageView.Height ;

_bitmap = _file.Path.LoadAndResizeBitmap (width, height);
if (_bitmap != null) {
_imageView.SetImageBitmap (_bitmap); //_imageView is instance variables
_bitmap = null;
}

// Dispose of the Java side bitmap.
GC.Collect();

}

I have also add permissions for that :







I am getting error "no non-static method "Landroid/app/fragment;.getContext()Landroid/content/Context;" on kitkat and lollipop" in second line of below code .

private bool IsThereAnAppToTakePictures ()
{
Intent intent = new Intent (MediaStore.ActionImageCapture);
IList availableActivities = Context.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}

I have tried "this.Activity" instead of "Context" but in this case i am getting null in "_file" at line " Uri contentUri = Uri.FromFile (_file);"

Please suggest me on this issue.

Best Answers

  • Dev_MaranDev_Maran US ✭✭
    edited May 2017 Accepted Answer

    yes, i have include these permissions









Answers

  • Dev_MaranDev_Maran USMember ✭✭
    edited May 2017 Accepted Answer

    yes, i have include these permissions









Sign In or Register to comment.