Skip to content

Commit c404062

Browse files
CopilotTomTasche
andcommitted
Fix recent documents SecurityException by filtering inaccessible URIs
Co-authored-by: TomTasche <128734+TomTasche@users.noreply.github.com>
1 parent 8e4ea7e commit c404062

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

app/src/main/java/at/tomtasche/reader/background/RecentDocumentsUtil.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public static Map<String, String> getRecentDocuments(Context context)
3030
String filename = document.getString("filename");
3131
String uri = document.getString("uri");
3232

33-
result.put(filename, uri);
33+
// Only include URIs that are still accessible
34+
if (isUriAccessible(context, Uri.parse(uri))) {
35+
result.put(filename, uri);
36+
}
3437
}
3538

3639
return result;
@@ -134,4 +137,19 @@ private static int findUriIndex(String uriString, JSONArray jsonArray) throws JS
134137
}
135138
return index;
136139
}
140+
141+
/**
142+
* Check if a URI is still accessible to the app.
143+
* This helps filter out URIs that no longer have valid permissions.
144+
*/
145+
private static boolean isUriAccessible(Context context, Uri uri) {
146+
try {
147+
// Try to open an input stream to test accessibility
148+
context.getContentResolver().openInputStream(uri).close();
149+
return true;
150+
} catch (Exception e) {
151+
// URI is not accessible (SecurityException, FileNotFoundException, etc.)
152+
return false;
153+
}
154+
}
137155
}

0 commit comments

Comments
 (0)