zoukankan      html  css  js  c++  java
  • xamarin.android之 Android 4.4+ 获取图片真实路径

    Android 4.4以下 选择图片是可以获取到图片路径的.高于Android 4.4获取图片路径只是获取到一个图片编号. 所以需要针对Android版本进行路径解析:

            #region 高于 v4.4 版本 解析真实路径
    public static String GetPath(Context context,Android.Net.Uri uri) { bool isKitKat = Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat; // DocumentProvider if (isKitKat && DocumentsContract.IsDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { String docId = DocumentsContract.GetDocumentId(uri); String[] split = docId.Split(':'); String type = split[0]; if ("primary".Equals(type.ToLower())) { return Android.OS.Environment.ExternalStorageDirectory + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { String id = DocumentsContract.GetDocumentId(uri); Android.Net.Uri contentUri = ContentUris.WithAppendedId( Android.Net.Uri.Parse("content://downloads/public_downloads"),long.Parse(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { String docId = DocumentsContract.GetDocumentId(uri); String[] split = docId.Split(':'); String type = split[0]; Android.Net.Uri contentUri = null; if ("image".Equals(type)) { contentUri = MediaStore.Images.Media.ExternalContentUri; } else if ("video".Equals(type)) { contentUri = MediaStore.Video.Media.ExternalContentUri; } else if ("audio".Equals(type)) { contentUri = MediaStore.Audio.Media.ExternalContentUri; } String selection = "_id=?"; String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".Equals(uri.Scheme.ToLower())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.LastPathSegment; return getDataColumn(context, uri, null, null); } // File else if ("file".Equals(uri.Scheme.ToLower())) { return uri.Path; } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context,Android.Net.Uri uri, String selection, String[] selectionArgs) { ICursor cursor = null; String column = "_data"; String[] projection = { column }; try { cursor = context.ContentResolver.Query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.MoveToFirst()) { int index = cursor.GetColumnIndexOrThrow(column); return cursor.GetString(index); } } finally { if (cursor != null) cursor.Close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static bool isExternalStorageDocument(Android.Net.Uri uri) { return "com.android.externalstorage.documents".Equals(uri.Authority); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static bool isDownloadsDocument(Android.Net.Uri uri) { return "com.android.providers.downloads.documents".Equals(uri.Authority); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static bool isMediaDocument(Android.Net.Uri uri) { return "com.android.providers.media.documents".Equals(uri.Authority); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static bool isGooglePhotosUri(Android.Net.Uri uri) { return "com.google.android.apps.photos.content".Equals(uri.Authority); } #endregion
  • 相关阅读:
    利用CSS计数函数counter()实现计数
    弹跳加载动画特效Bouncing loader
    HTML页面中解决内容元素随窗口变化布局变乱问题
    CSS中列表项list样式
    框模型中设置内容区域元素占地尺寸box-sizing属性
    PHP100视频教程-->视频下载
    HTML页面中5种超酷的伪类选择器:hover效果
    HTML中获取input中单选按钮radio数据(性别例子)
    HTML中 DOM操作的Document 对象详解(收藏)
    14、输入一个链表,从尾到头打印链表每个节点的值。
  • 原文地址:https://www.cnblogs.com/mycing/p/5660090.html
Copyright © 2011-2022 走看看