Java 如何将图像上载设置为仅jpg和png文件?
下面是我正在使用的代码,它工作得很好,但是如何将文件类型设置为jpg和png,以及不允许/不显示库中的任何其他图像Java 如何将图像上载设置为仅jpg和png文件?,java,c#,android,xamarin,xamarin.android,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); StartActivityForRe
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);
}
}
使用这个
Intent.setType(“image/jpg”)
而不是Intent.setType(“image/*”)代码>对jpeg使用setType
intent.setType(“image/jpeg”)
或intent.setType(“image/jpg”)
还是巴布亚新几内亚
intent.setType(“image/png”)
我认为所有给出的答案都是错误的。要求是同时允许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);
但我如何允许oth jpg和png?只有那些格式而没有其他格式?但是当我尝试这个时,当我将它设置为intent.setType(“image/jpg”)时,它仍然允许选择其他格式。