Skip to content

Commit 7d8927e

Browse files
committed
add some more hacky ways to get path
1 parent 2edf7f8 commit 7d8927e

File tree

1 file changed

+22
-7
lines changed
  • app/src/main/java/com/amaze/filemanager/filesystem/files

1 file changed

+22
-7
lines changed

app/src/main/java/com/amaze/filemanager/filesystem/files/UriUtils.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,14 @@ fun fromUri(uri: Uri, context: Context): String? {
8484
return uri.lastPathSegment
8585
}
8686
val path = getDataColumn(context, uri, null, null)
87-
val uriPath = uri.path
8887
if (path != null) {
8988
return path
9089
} else if (fileExists(uri.path)) {
9190
// Check if the full path is the uri path
9291
return uri.path
93-
} else if (uriPath != null && uriPath.contains("/storage")) {
94-
// As last resort, check if the full path is somehow contained in the uri
95-
val pathInUri = uriPath.substring(uriPath.indexOf("/storage"))
96-
if (fileExists(pathInUri)) {
97-
return pathInUri
98-
}
92+
} else {
93+
// Check if the full path is contained in the uri path
94+
return getPathInUri(uri)
9995
}
10096
}
10197
if ("file".equals(uri.scheme, ignoreCase = true)) {
@@ -233,6 +229,25 @@ private fun getDataColumn(
233229
return null
234230
}
235231

232+
private fun getPathInUri(uri: Uri): String? {
233+
// As last resort, check if the full path is somehow contained in the uri path
234+
val uriPath = uri.path ?: return null
235+
// Some common path prefixes
236+
val pathPrefixes = listOf("/storage", "/external_files")
237+
for (prefix in pathPrefixes) {
238+
if (uriPath.contains(prefix)) {
239+
// make sure path starts with storage
240+
val pathInUri = "/storage${uriPath.substring(
241+
uriPath.indexOf(prefix) + prefix.length
242+
)}"
243+
if (fileExists(pathInUri)) {
244+
return pathInUri
245+
}
246+
}
247+
}
248+
return null
249+
}
250+
236251
private fun isExternalStorageDocument(uri: Uri): Boolean {
237252
return "com.android.externalstorage.documents" == uri.authority
238253
}

0 commit comments

Comments
 (0)