Skip to content

Commit

Permalink
add some more hacky ways to get path
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Feb 3, 2024
1 parent 2edf7f8 commit 7d8927e
Showing 1 changed file with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,14 @@ fun fromUri(uri: Uri, context: Context): String? {
return uri.lastPathSegment
}
val path = getDataColumn(context, uri, null, null)
val uriPath = uri.path
if (path != null) {
return path
} else if (fileExists(uri.path)) {
// Check if the full path is the uri path
return uri.path
} else if (uriPath != null && uriPath.contains("/storage")) {
// As last resort, check if the full path is somehow contained in the uri
val pathInUri = uriPath.substring(uriPath.indexOf("/storage"))
if (fileExists(pathInUri)) {
return pathInUri
}
} else {
// Check if the full path is contained in the uri path
return getPathInUri(uri)
}
}
if ("file".equals(uri.scheme, ignoreCase = true)) {
Expand Down Expand Up @@ -233,6 +229,25 @@ private fun getDataColumn(
return null
}

private fun getPathInUri(uri: Uri): String? {
// As last resort, check if the full path is somehow contained in the uri path
val uriPath = uri.path ?: return null
// Some common path prefixes
val pathPrefixes = listOf("/storage", "/external_files")
for (prefix in pathPrefixes) {
if (uriPath.contains(prefix)) {
// make sure path starts with storage
val pathInUri = "/storage${uriPath.substring(
uriPath.indexOf(prefix) + prefix.length
)}"
if (fileExists(pathInUri)) {
return pathInUri
}
}
}
return null
}

private fun isExternalStorageDocument(uri: Uri): Boolean {
return "com.android.externalstorage.documents" == uri.authority
}
Expand Down

0 comments on commit 7d8927e

Please sign in to comment.