Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set image upload to only jpg and png Files?

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);
    }
}
like image 482
Ash Avatar asked Mar 18 '15 04:03

Ash


1 Answers

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);
like image 162
Dinith Rukshan Kumara Avatar answered Oct 22 '22 01:10

Dinith Rukshan Kumara