GetRealPath for image Android10

问题描述 投票:-1回答:1

[我正在尝试获取Android 10中Picked图像的真实路径,但使用以下代码无法正常工作Iam在Android 9中可以正常工作

public static String getRealPathFromURI(Context context, Android.Net.Uri contentUri)
        {
            ICursor cursor = context.ContentResolver.Query(contentUri, null, null, null, null);
            cursor.MoveToFirst();
            String document_id = cursor.GetString(0);
            document_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
            cursor.Close();

            cursor = context.ContentResolver.Query(
            Android.Provider.MediaStore.Images.Media.ExternalContentUri,
            null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new string[] { document_id }, null);
            cursor.MoveToFirst();
            String path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));

             cursor.Close();
            return path;
        }
android xamarin.android
1个回答
0
投票
选项2

您可以从nuget安装插件

Xam.Plugin.Media

用法

await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { return; } var file = await CrossMedia.Current.PickPhotoAsync(); if (file == null) return; var path = file.Path;

有关该插件的更多详细信息,您可以检查https://github.com/jamesmontemagno/MediaPlugin

© www.soinside.com 2019 - 2024. All rights reserved.