如何将图片上传到jpg和png文件?

时间:2015-03-18 04:21:26

标签: java c# android xamarin xamarin.android

以下是我使用的代码很有效,但我如何只将文件类型设置为jpg和png,并禁止/不显示库中的任何其他图像

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);
    }
}

3 个答案:

答案 0 :(得分:2)

我认为所有给出的答案都是错误的。要求是同时允许jpg和png文件。这仅允许选择给定的文件类型。

首先创建包括所有允许文件类型的mimeTypes数组。
然后将其放入意向中。

这是代码。

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);

答案 1 :(得分:0)

使用此Intent.setType("image/jpg");代替Intent.setType("image/*");

答案 2 :(得分:0)

使用setType for jpeg

intent.setType("image/jpeg")intent.setType("image/jpg")

或png

intent.setType("image/png")