Skip to content

Commit f384c92

Browse files
committed
Fix crash when opening Recent Files without storage permission
Wrap MediaStore query in listRecentFiles() with try/catch for SecurityException. When READ_EXTERNAL_STORAGE is denied, return the current list instead of letting the exception escape doInBackground() and crash the app.
1 parent d998976 commit f384c92

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -551,29 +551,34 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
551551
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2);
552552
Date d = c.getTime();
553553
Cursor cursor;
554-
if (SDK_INT >= Q) {
555-
Bundle queryArgs = new Bundle();
556-
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
557-
queryArgs.putStringArray(
558-
ContentResolver.QUERY_ARG_SORT_COLUMNS,
559-
new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED});
560-
queryArgs.putInt(
561-
ContentResolver.QUERY_ARG_SORT_DIRECTION,
562-
ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
563-
cursor =
564-
context
565-
.getContentResolver()
566-
.query(MediaStore.Files.getContentUri("external"), projection, queryArgs, null);
567-
} else {
568-
cursor =
569-
context
570-
.getContentResolver()
571-
.query(
572-
MediaStore.Files.getContentUri("external"),
573-
projection,
574-
null,
575-
null,
576-
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20");
554+
try {
555+
if (SDK_INT >= Q) {
556+
Bundle queryArgs = new Bundle();
557+
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
558+
queryArgs.putStringArray(
559+
ContentResolver.QUERY_ARG_SORT_COLUMNS,
560+
new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED});
561+
queryArgs.putInt(
562+
ContentResolver.QUERY_ARG_SORT_DIRECTION,
563+
ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
564+
cursor =
565+
context
566+
.getContentResolver()
567+
.query(MediaStore.Files.getContentUri("external"), projection, queryArgs, null);
568+
} else {
569+
cursor =
570+
context
571+
.getContentResolver()
572+
.query(
573+
MediaStore.Files.getContentUri("external"),
574+
projection,
575+
null,
576+
null,
577+
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20");
578+
}
579+
} catch (SecurityException e) {
580+
// Storage permission denied; treat as no recent files instead of crashing.
581+
return recentFiles;
577582
}
578583
if (cursor == null) return recentFiles;
579584
if (cursor.getCount() > 0 && cursor.moveToFirst()) {

0 commit comments

Comments
 (0)