根据URI获取图片路径

野性酷女 2022-05-16 14:35 330阅读 0赞
  1. import android.annotation.SuppressLint;
  2. import android.content.ContentUris;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.provider.DocumentsContract;
  8. import android.provider.MediaStore;
  9. /**
  10. * Created by 2018-10-27 on 12:04.
  11. * Dscription
  12. * Author by Administrator
  13. */
  14. public class ImageUtil {
  15. /**
  16. * 根据Uri获取图片的绝对路径
  17. *
  18. * @param context 上下文对象
  19. * @param uri 图片的Uri
  20. * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
  21. */
  22. public static String getRealPathFromUri(Context context, Uri uri) {
  23. int sdkVersion = Build.VERSION.SDK_INT;
  24. if (sdkVersion >= 19) { // api >= 19
  25. return getRealPathFromUriAboveApi19(context, uri);
  26. } else { // api < 19
  27. return getRealPathFromUriBelowAPI19(context, uri);
  28. }
  29. }
  30. /**
  31. * 适配api19以下(不包括api19),根据uri获取图片的绝对路径
  32. *
  33. * @param context 上下文对象
  34. * @param uri 图片的Uri
  35. * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
  36. */
  37. private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) {
  38. return getDataColumn(context, uri, null, null);
  39. }
  40. /**
  41. * 适配api19及以上,根据uri获取图片的绝对路径
  42. *
  43. * @param context 上下文对象
  44. * @param uri 图片的Uri
  45. * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
  46. */
  47. @SuppressLint("NewApi")
  48. private static String getRealPathFromUriAboveApi19(Context context, Uri uri) {
  49. String filePath = null;
  50. if (DocumentsContract.isDocumentUri(context, uri)) {
  51. // 如果是document类型的 uri, 则通过document id来进行处理
  52. String documentId = DocumentsContract.getDocumentId(uri);
  53. if (isMediaDocument(uri)) { // MediaProvider
  54. // 使用':'分割
  55. String id = documentId.split(":")[1];
  56. String selection = MediaStore.Images.Media._ID + "=?";
  57. String[] selectionArgs = {id};
  58. filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
  59. } else if (isDownloadsDocument(uri)) { // DownloadsProvider
  60. Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
  61. filePath = getDataColumn(context, contentUri, null, null);
  62. }
  63. } else if ("content".equalsIgnoreCase(uri.getScheme())){
  64. // 如果是 content 类型的 Uri
  65. filePath = getDataColumn(context, uri, null, null);
  66. } else if ("file".equals(uri.getScheme())) {
  67. // 如果是 file 类型的 Uri,直接获取图片对应的路径
  68. filePath = uri.getPath();
  69. }
  70. return filePath;
  71. }
  72. /**
  73. * 获取数据库表中的 _data 列,即返回Uri对应的文件路径
  74. * @return
  75. */
  76. private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  77. String path = null;
  78. String[] projection = new String[]{MediaStore.Images.Media.DATA};
  79. Cursor cursor = null;
  80. try {
  81. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
  82. if (cursor != null && cursor.moveToFirst()) {
  83. int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
  84. path = cursor.getString(columnIndex);
  85. }
  86. } catch (Exception e) {
  87. if (cursor != null) {
  88. cursor.close();
  89. }
  90. }
  91. return path;
  92. }
  93. /**
  94. * @param uri the Uri to check
  95. * @return Whether the Uri authority is MediaProvider
  96. */
  97. private static boolean isMediaDocument(Uri uri) {
  98. return "com.android.providers.media.documents".equals(uri.getAuthority());
  99. }
  100. /**
  101. * @param uri the Uri to check
  102. * @return Whether the Uri authority is DownloadsProvider
  103. */
  104. private static boolean isDownloadsDocument(Uri uri) {
  105. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  106. }
  107. }

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读