Skip to content

Instantly share code, notes, and snippets.

@baileysh9
Last active January 24, 2018 20:07
Show Gist options
  • Save baileysh9/6e20b11fd66c3517fef3bd544181a2a2 to your computer and use it in GitHub Desktop.
Save baileysh9/6e20b11fd66c3517fef3bd544181a2a2 to your computer and use it in GitHub Desktop.
Launching Camera and Photo Gallery in Xamarin Android
public enum FileFormatEnum
{
PNG,
JPEG
}
public void BringUpCamera(string imageId, FileFormatEnum fileType)
{
var intent = new Intent(MediaStore.ActionImageCapture);
Intent takePictureIntent = new Intent(MediaStore.ActionImageCapture);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.ResolveActivity(Forms.Context.PackageManager) != null)
{
try
{
var documentsDirectry = Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures);
cameraFile = new Java.IO.File(documentsDirectry, imageId + "." + fileType.ToString());
if (cameraFile != null)
{
using (var mediaStorageDir = new Java.IO.File(documentsDirectry, string.Empty))
{
if (!mediaStorageDir.Exists())
{
if (!mediaStorageDir.Mkdirs())
throw new IOException("Couldn't create directory, have you added the WRITE_EXTERNAL_STORAGE permission?");
}
}
}
}
catch (IOException ex)
{
// Error occurred while creating the File
App.WriteOutput(ex.ToString());
}
//NOTE: Make sure the authority matches what you put in the AndroidManifest.xml file
Android.Net.Uri photoURI = FileProvider.GetUriForFile(((Activity)Forms.Context), "com.yourcompany.appname.fileprovider", cameraFile);
takePictureIntent.PutExtra(MediaStore.ExtraOutput, photoURI);
((Activity)Forms.Context).StartActivityForResult(takePictureIntent, 0);
}
}
public void BringUpPhotoGallery(string imageId, FileFormatEnum fileType)
{
var imageIntent = new Intent();
imageIntent.SetType("image/*");
if (imageId != null)
{
imageIntent.PutExtra("fileName", imageId + "." + fileType.ToString());
}
imageIntent.SetAction(Intent.ActionGetContent);
((Activity)Forms.Context).StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment