From 7d8927efed5c11bb40650a933648eddc7d8a7f46 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Sat, 3 Feb 2024 23:49:12 +0100 Subject: [PATCH] add some more hacky ways to get path --- .../filemanager/filesystem/files/UriUtils.kt | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/filesystem/files/UriUtils.kt b/app/src/main/java/com/amaze/filemanager/filesystem/files/UriUtils.kt index 4ce33c3b46..e90a64abe9 100644 --- a/app/src/main/java/com/amaze/filemanager/filesystem/files/UriUtils.kt +++ b/app/src/main/java/com/amaze/filemanager/filesystem/files/UriUtils.kt @@ -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)) { @@ -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 }