Here is the code i am using it works great but how to i only set the files types to jpg and png and disallow/ not display any other images in the gallery
private void ButtonOnClick(object sender, EventArgs eventArgs) {
Intent = new Intent();
Intent.SetType("image/*");
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
}
#endregion
#region Get the Path of Selected Image
private string GetPathToImage(Uri uri) {
string path = null;
// The projection contains the columns we want to return in our query.
string[] projection = new[] {
Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
using (ICursor cursor = ManagedQuery(uri, projection, null, null, null)) {
if (cursor != null) {
int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
}
return path;
}
#endregion
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) {
// For single image Selection
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null)) {
Uri uri = data.Data;
_imageView.SetImageURI(uri);
path = GetPathToImage (uri);
}
}
I think all the given answers are wrong. Requirement is to allow both jpg and png Files. This allows only to select given file types.
First Create mimeTypes array including all the allow file types.
Then put it in intent extras.
Here is the code.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String [] mimeTypes = {"image/png", "image/jpg","image/jpeg"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_GET_SINGLE_FILE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With