Skip to content

Commit 3991b65

Browse files
committed
Fix the fromContentUri null/short path causes substring crash
1 parent 8829036 commit 3991b65

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

  • app/src/main/java/com/amaze/filemanager/filesystem/files

app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,38 @@ public static File fromContentUri(@NonNull Uri uri) {
10151015
if (!CONTENT.name().equalsIgnoreCase(uri.getScheme())) {
10161016
LOG.warn("URI must start with content://. URI was [" + uri + "]");
10171017
}
1018-
File pathFile = new File(uri.getPath().substring(FILE_PROVIDER_PREFIX.length() + 1));
1018+
1019+
String path = uri.getPath();
1020+
if (path == null) {
1021+
LOG.warn("Uri.getPath() returned null for uri [{}]", uri);
1022+
return null;
1023+
}
1024+
1025+
// Expect paths like "/<FILE_PROVIDER_PREFIX>/..." (note leading slash)
1026+
final String expectedPrefix = "/" + FILE_PROVIDER_PREFIX + "/";
1027+
File pathFile = null;
1028+
1029+
if (path.startsWith(expectedPrefix) && path.length() > expectedPrefix.length()) {
1030+
// safe substring only when prefix present and there's remaining text
1031+
String relative = path.substring(expectedPrefix.length());
1032+
pathFile = new File(relative);
1033+
LOG.debug("Parsed provider-relative path [{}] from uri [{}]", relative, uri);
1034+
} else {
1035+
// unexpected shape — log and fall back to using the full path
1036+
LOG.warn(
1037+
"URI path [{}] doesn't start with expected prefix [{}]. Using full path as fallback.",
1038+
path,
1039+
expectedPrefix);
1040+
pathFile = new File(path);
1041+
}
1042+
10191043
if (!pathFile.exists()) {
10201044
LOG.warn("Failed to navigate to the initial path: {}", pathFile.getPath());
1021-
pathFile = new File(uri.getPath());
1022-
LOG.warn("Attempting to navigate to the fallback path: {}", pathFile.getPath());
1045+
File fallback = new File(path);
1046+
LOG.warn("Attempting to navigate to the fallback path: {}", fallback.getPath());
1047+
return fallback;
10231048
}
1049+
10241050
return pathFile;
10251051
}
10261052

0 commit comments

Comments
 (0)