问题描述
我正在尝试获取图库图像的路径.我正在获取存储在内部存储中而不是外部存储中的图像的路径.我还启用了已授予的读写存储和相机访问权限.这是我的代码
I am trying to get the path of Gallery Image. I am getting the path of the image which is stored in internal storage but not of the external storage. I have also enabled the read-write storage and camera access permissions which has been granted.Here is my code
void ChoosePhoto()
{
try
{
var imageIntent = new Intent();
imageIntent.SetType("image/*");
imageIntent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), 0);
}
catch (Exception ex)
{
throw ex;
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE)
{
// camera operation
}
else
{
Console.WriteLine("choose pic from Gallery");
var uri = data.Data;
var path = getRealPathFromURI(uri); //path returns null when i select image from external storage;
}
}
private String getRealPathFromURI(Uri contentURI)
{
string[] proj = { MediaStore.Images.ImageColumns.Data };
String result;
var cursor = ContentResolver.Query(contentURI, proj, null, null, null);
if (cursor == null)
result = contentURI.Path;
else
{
int idx= cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
cursor.MoveToFirst();
result = cursor.GetString(idx);
cursor.Close();
}
return result;
}
推荐答案
问题是将Android.Net.Uri
改成path
,解决办法如下:
The problem is that change Android.Net.Uri
to path
, here is an solution :
private string GetPathToImage(Android.Net.Uri uri)
{
string doc_id = "";
using (var c1 = ContentResolver.Query(uri, null, null, null, null))
{
c1.MoveToFirst();
string document_id = c1.GetString(0);
doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
}
string path = null;
// The projection contains the columns we want to return in our query.
string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
using (var cursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null))
{
if (cursor == null) return path;
var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
return path;
}
当您从图库中选择图片时:
When you choose a picture from gallery :
ChoosePhoto();
...
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
var uri = data.Data;
var path = GetPathToImage(uri);
System.Diagnostics.Debug.WriteLine("Image path == " + path );
//My result is this ==> [0:] Image path == /storage/emulated/0/DCIM/Camera/IMG_20170813_223324.jpg
}
}
最重要的,当你使用这个方法时,也许你会遇到这个问题:
Most important, when you use this method, maybe you will come across this problem :
Java.Lang.SecurityException:权限拒绝:从 pid=21975、uid=10417 读取 com.android.providers.media.MediaProvider uri content://media/external/images/media 需要 android.permission.READ_EXTERNAL_STORAGE,或grantUriPermission()
当 API >= 23 时,在运行时请求权限,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时授予权限.你应该检查你是否有android.permission.READ_EXTERNAL_STORAGE
权限,如果没有,你需要请求权限.
When API >= 23, Requesting Permissions at Run Time, users grant permissions to apps while the app is running, not when they install the app. You should check if you have android.permission.READ_EXTERNAL_STORAGE
permission, if not, you need to request the permissions.
这篇关于获取图库图像 Xamarin 的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!