catch exceptions thrown at looking up contacts

This commit is contained in:
tibbi 2018-02-10 16:49:50 +01:00
parent 3c834fe521
commit b8b118c62c
1 changed files with 9 additions and 3 deletions

View File

@ -55,7 +55,9 @@ fun Context.getLookupUriRawId(dataUri: Uri): Int {
val lookupKey = getLookupKeyFromUri(dataUri) val lookupKey = getLookupKeyFromUri(dataUri)
if (lookupKey != null && isLollipopPlus()) { if (lookupKey != null && isLollipopPlus()) {
val uri = lookupContactUri(lookupKey, this) val uri = lookupContactUri(lookupKey, this)
return getContactUriRawId(uri) if (uri != null) {
getContactUriRawId(uri)
}
} }
return -1 return -1
} }
@ -94,9 +96,13 @@ fun isEncodedContactUri(uri: Uri?): Boolean {
return lastPathSegment == "encoded" return lastPathSegment == "encoded"
} }
fun lookupContactUri(lookup: String, context: Context): Uri { fun lookupContactUri(lookup: String, context: Context): Uri? {
val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup) val lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup)
return ContactsContract.Contacts.lookupContact(context.contentResolver, lookupUri) return try {
ContactsContract.Contacts.lookupContact(context.contentResolver, lookupUri)
} catch (e: Exception) {
null
}
} }
fun Context.getCachePhoto(): File { fun Context.getCachePhoto(): File {