您也可以嘗試使用這個,如果你使用>奇巧
public static String getRealPathFromURI_API19(Context context, Android.Net.Uri uri)
{
String filePath = "";
// ExternalStorageProvider
if (isExternalStorageDocument(uri))
{
String docId = DocumentsContract.GetDocumentId(uri);
String[] split = docId.Split(':');
String type = split[0];
if ("primary".Equals(type, StringComparison.OrdinalIgnoreCase))
{
return Android.OS.Environment.ExternalStorageDirectory + "/" + split[1];
}
else
{
if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
{
//getExternalMediaDirs() added in API 21
File[] extenal = context.GetExternalMediaDirs();
if (extenal.Length > 1)
{
filePath = extenal[1].AbsolutePath;
filePath = filePath.Substring(0, filePath.IndexOf("Android")) + split[1];
//File f = new File(extenal[1].Parent + File.Separator + "ddddddd.amit");
//var created= f.CreateNewFile();
}
}
else
{
filePath = "/storage/" + type + "/" + split[1];
}
return filePath;
}
}
else if (isDownloadsDocument(uri))
{
// DownloadsProvider
String id = DocumentsContract.GetDocumentId(uri);
//final Uri contentUri = ContentUris.withAppendedId(
// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
//Cursor cursor = null;
String column = "_data";
String[] projection = { column };
var data = GetDataColumn(context, uri, column, projection);
return data;
}
else if (DocumentsContract.IsDocumentUri(context, uri))
{
// MediaProvider
String wholeID = DocumentsContract.GetDocumentId(uri);
// Split at colon, use second item in the array
String[] ids = wholeID.Split(':');
String id;
String type;
if (ids.Length > 1)
{
id = ids[1];
type = ids[0];
}
else
{
id = ids[0];
type = ids[0];
}
Android.Net.Uri contentUri = null;
if ("image".Equals(type, StringComparison.OrdinalIgnoreCase))
{
contentUri = MediaStore.Images.Media.ExternalContentUri;
}
else if ("video".Equals(type, StringComparison.OrdinalIgnoreCase))
{
contentUri = MediaStore.Video.Media.ExternalContentUri;
}
else if ("audio".Equals(type, StringComparison.OrdinalIgnoreCase))
{
contentUri = MediaStore.Audio.Media.ExternalContentUri;
}
String selection = "_id=?";
String[] selectionArgs = new String[] { id };
String column = "_data";
String[] projection = { column };
var data = GetDataColumn(context, uri, column, projection);
return data;
}
else
{
//commented------------------------------
//String[] proj = { MediaStore.Audio.Media.DATA };
//Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
//if (cursor != null)
//{
// int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
// if (cursor.moveToFirst())
// filePath = cursor.getString(column_index);
// cursor.close();
//}
return filePath;
}
return null;
}
這不是完整的「返回URI」。 – greenapps
其中一個完整的uri是「內容://com.android.externalstorage.documents/document/home%3Ademo_decry.mp4」 – sohnyi