FileUtils.kt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package uts.sdk.modules.uniChooseSystemImage
  2. import android.content.ContentResolver
  3. import android.content.ContentUris
  4. import android.content.Context
  5. import android.database.Cursor
  6. import android.net.Uri
  7. import android.os.Build
  8. import android.os.Environment
  9. import android.provider.DocumentsContract
  10. import android.provider.MediaStore
  11. object FileUtils {
  12. fun getFilePathByUri(context: Context, uri: Uri): String? {
  13. var path: String? = null
  14. // 以 file:// 开头的
  15. if (ContentResolver.SCHEME_FILE == uri.scheme) {
  16. path = uri.path
  17. return path
  18. }
  19. // 以 content:// 开头的,比如 content://media/extenral/images/media/17766
  20. if (ContentResolver.SCHEME_CONTENT == uri.scheme && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  21. val cursor = context.contentResolver.query(
  22. uri,
  23. arrayOf(MediaStore.Images.Media.DATA),
  24. null,
  25. null,
  26. null
  27. )
  28. if (cursor != null) {
  29. if (cursor.moveToFirst()) {
  30. val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
  31. if (columnIndex > -1) {
  32. path = cursor.getString(columnIndex)
  33. }
  34. }
  35. cursor.close()
  36. }
  37. return path
  38. }
  39. // 4.4及之后的 是以 content:// 开头的,比如 content://com.android.providers.media.documents/document/image%3A235700
  40. if (ContentResolver.SCHEME_CONTENT == uri.scheme && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  41. if (DocumentsContract.isDocumentUri(context, uri)) {
  42. if (isExternalStorageDocument(uri)) {
  43. // ExternalStorageProvider
  44. val docId = DocumentsContract.getDocumentId(uri)
  45. val split =
  46. docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
  47. val type = split[0]
  48. if ("primary".equals(type, ignoreCase = true)) {
  49. path = Environment.getExternalStorageDirectory().toString() + "/" + split[1]
  50. return path
  51. }
  52. } else if (isDownloadsDocument(uri)) {
  53. // DownloadsProvider
  54. val id = DocumentsContract.getDocumentId(uri)
  55. val contentUri = ContentUris.withAppendedId(
  56. Uri.parse("content://downloads/public_downloads"),
  57. id.toLong()
  58. )
  59. path = getDataColumn(context, contentUri, null, null)
  60. return path
  61. } else if (isMediaDocument(uri)) {
  62. // MediaProvider
  63. val docId = DocumentsContract.getDocumentId(uri)
  64. val split =
  65. docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
  66. val type = split[0]
  67. var contentUri: Uri? = null
  68. if ("image" == type) {
  69. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
  70. } else if ("video" == type) {
  71. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
  72. } else if ("audio" == type) {
  73. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
  74. }
  75. val selection = "_id=?"
  76. val selectionArgs = arrayOf(split[1])
  77. path = getDataColumn(context, contentUri, selection, selectionArgs)
  78. return path
  79. }
  80. }
  81. }
  82. return null
  83. }
  84. private fun getDataColumn(
  85. context: Context,
  86. uri: Uri?,
  87. selection: String?,
  88. selectionArgs: Array<String>?,
  89. ): String? {
  90. var cursor: Cursor? = null
  91. val column = "_data"
  92. val projection = arrayOf(column)
  93. try {
  94. cursor =
  95. context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
  96. if (cursor != null && cursor.moveToFirst()) {
  97. val column_index = cursor.getColumnIndexOrThrow(column)
  98. return cursor.getString(column_index)
  99. }
  100. } finally {
  101. cursor?.close()
  102. }
  103. return null
  104. }
  105. private fun isExternalStorageDocument(uri: Uri): Boolean {
  106. return "com.android.externalstorage.documents" == uri.authority
  107. }
  108. private fun isDownloadsDocument(uri: Uri): Boolean {
  109. return "com.android.providers.downloads.documents" == uri.authority
  110. }
  111. private fun isMediaDocument(uri: Uri): Boolean {
  112. return "com.android.providers.media.documents" == uri.authority
  113. }
  114. }