Skip to content

Commit d7aedb2

Browse files
committed
Fatal Exception: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri
1 parent d24a40c commit d7aedb2

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

lib/src/main/java/com/ismartcoding/lib/extensions/ContentResolver.kt

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,79 @@ fun ContentResolver.setSystemScreenTimeout(timeout: Int): Boolean {
3737
}
3838

3939
fun ContentResolver.count(uri: Uri, where: ContentWhere): Int {
40-
return if (isQPlus()) {
41-
countWithBundle(uri, where)
42-
} else {
43-
try {
44-
countWithSql(uri, where)
45-
} catch (ex: Exception) {
46-
// Fatal Exception: java.lang.IllegalArgumentException: Non-token detected in 'count(*) AS count'
40+
return try {
41+
if (isQPlus()) {
4742
countWithBundle(uri, where)
43+
} else {
44+
try {
45+
countWithSql(uri, where)
46+
} catch (ex: Exception) {
47+
// Fatal Exception: java.lang.IllegalArgumentException: Non-token detected in 'count(*) AS count'
48+
countWithBundle(uri, where)
49+
}
4850
}
51+
} catch (ex: SecurityException) {
52+
// Permission Denial: reading MediaProvider without proper permissions
53+
LogCat.e("Permission denied when querying MediaStore: ${ex.message}")
54+
0
55+
} catch (ex: Exception) {
56+
LogCat.e("Error querying MediaStore: ${ex.message}")
57+
0
4958
}
5059
}
5160

5261
fun ContentResolver.countWithSql(uri: Uri, where: ContentWhere): Int {
53-
var result = 0
54-
query(
55-
uri,
56-
arrayOf("count(*) AS count"),
57-
where.toSelection(),
58-
where.args.toTypedArray(),
59-
null,
60-
)?.use {
61-
it.moveToFirst()
62-
if (it.count > 0) {
63-
result = it.getInt(0)
62+
return try {
63+
var result = 0
64+
query(
65+
uri,
66+
arrayOf("count(*) AS count"),
67+
where.toSelection(),
68+
where.args.toTypedArray(),
69+
null,
70+
)?.use {
71+
it.moveToFirst()
72+
if (it.count > 0) {
73+
result = it.getInt(0)
74+
}
6475
}
76+
result
77+
} catch (ex: SecurityException) {
78+
LogCat.e("Permission denied when querying MediaStore with SQL: ${ex.message}")
79+
0
80+
} catch (ex: Exception) {
81+
LogCat.e("Error querying MediaStore with SQL: ${ex.message}")
82+
0
6583
}
66-
67-
return result
6884
}
6985

7086
fun ContentResolver.countWithBundle(uri: Uri, where: ContentWhere): Int {
71-
var result = 0
72-
query(
73-
uri,
74-
null,
75-
Bundle().apply {
76-
where(where.toSelection(), where.args)
77-
if (where.trash == true) {
78-
if (isRPlus()) {
79-
putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
87+
return try {
88+
var result = 0
89+
query(
90+
uri,
91+
null,
92+
Bundle().apply {
93+
where(where.toSelection(), where.args)
94+
if (where.trash == true) {
95+
if (isRPlus()) {
96+
putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
97+
}
8098
}
81-
}
82-
},
83-
null,
84-
)?.use {
85-
it.moveToFirst()
86-
result = it.count
99+
},
100+
null,
101+
)?.use {
102+
it.moveToFirst()
103+
result = it.count
104+
}
105+
result
106+
} catch (ex: SecurityException) {
107+
LogCat.e("Permission denied when querying MediaStore with bundle: ${ex.message}")
108+
0
109+
} catch (ex: Exception) {
110+
LogCat.e("Error querying MediaStore with bundle: ${ex.message}")
111+
0
87112
}
88-
89-
return result
90113
}
91114

92115
fun ContentResolver.getPagingCursor(

0 commit comments

Comments
 (0)