-
Summaryhere created file with timestamp https://github.com/dimonovdd/Xamarin.MediaGallery/blob/b9a26c94cf3c1065b6f0c8a81cbfd6b12c49b901/MediaGallery/MediaGallery/SaveMedia.android.cs how we can get this path or name? API Changes |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is almost unrealistic for a cross-platform implementation. We will have to request additional permissions, etc. Getting the file path is basically not consistent with the concept of iOS and android. |
Beta Was this translation helpful? Give feedback.
-
This is possible and I am using it (not encountered any problem so far). I don't know about iOS but in Android it can be implemented as: public static string GetRealPathFromURI(Android.Net.Uri contentURI)
{
ICursor cursor = null;
string mediaPath = string.Empty;
try
{
cursor = Platform.CurrentActivity.ContentResolver.Query(contentURI, null, null, null, null);
cursor.MoveToFirst();
int idx = cursor.GetColumnIndex(MediaStore.MediaColumns.Data);
if (idx != -1)
{
var type = Platform.CurrentActivity.ContentResolver.GetType(contentURI);
int pIdx = cursor.GetColumnIndex(MediaStore.MediaColumns.Id);
var mData = cursor.GetString(idx);
mediaPath = mData;
}
else
{
var docID = DocumentsContract.GetDocumentId(contentURI);
var doc = docID.Split(':');
var id = doc[1];
//var whereSelect = MediaStore.Images.ImageColumns.Id + "=?"; // Obsolete
var whereSelect = IBaseColumns.Id + "=?";
var dataConst = MediaStore.Images.ImageColumns.Data;
//dataConst = Platform.AppContext.ApplicationInfo.DataDir;
var projections = new string[] { dataConst };
var internalUri = MediaStore.Images.Media.InternalContentUri;
var externalUri = MediaStore.Images.Media.ExternalContentUri;
switch (doc[0])
{
case "video":
internalUri = MediaStore.Video.Media.InternalContentUri;
externalUri = MediaStore.Video.Media.ExternalContentUri;
whereSelect = MediaStore.Video.VideoColumns.Id + "=?";
dataConst = MediaStore.Video.VideoColumns.Data;
break;
case "image":
whereSelect = MediaStore.Video.VideoColumns.Id + "=?";
projections = new string[] { MediaStore.Video.VideoColumns.Data };
break;
}
projections = new string[] { dataConst };
cursor = Platform.CurrentActivity.ContentResolver.Query(internalUri, projections, whereSelect, new string[] { id }, null);
if (cursor.Count == 0)
{
cursor = Platform.CurrentActivity.ContentResolver.Query(externalUri, projections, whereSelect, new string[] { id }, null);
}
var colDatax = cursor.GetColumnIndexOrThrow(dataConst);
cursor.MoveToFirst();
mediaPath = cursor.GetString(colDatax);
}
}
catch (Exception ex)
{
Toast.MakeText(Platform.CurrentActivity, "Unable to get path. " + ex.Message, ToastLength.Long).Show();
}
finally
{
if (cursor != null)
{
cursor.Close();
cursor.Dispose();
}
}
return mediaPath;
} the |
Beta Was this translation helpful? Give feedback.
This is almost unrealistic for a cross-platform implementation. We will have to request additional permissions, etc. Getting the file path is basically not consistent with the concept of iOS and android.
You can also read this discussion: #37