在Xamarin Android中,可以通过以下步骤从图库中获取所选图片的名称:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Intent intent = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
intent.SetType("image/*");
StartActivityForResult(intent, 1);
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Result.Ok && data != null)
{
Android.Net.Uri selectedImageUri = data.Data;
string imageName = GetImageNameFromUri(selectedImageUri);
// 处理所选图片的名称
}
}
private string GetImageNameFromUri(Android.Net.Uri uri)
{
string[] projection = { Android.Provider.MediaStore.Images.ImageColumns.DisplayName };
var cursor = ContentResolver.Query(uri, projection, null, null, null);
if (cursor != null && cursor.MoveToFirst())
{
int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.ImageColumns.DisplayName);
string imageName = cursor.GetString(columnIndex);
cursor.Close();
return imageName;
}
return null;
}
以上步骤中,通过创建Intent并设置类型为image/*,然后启动图库活动并获取返回的URI,最后通过查询媒体库获取所选图片的名称。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云