From 1c1629da30bd1b125c59ab9bbcecff3bb3e74324 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Thu, 20 Aug 2009 08:13:46 -0700 Subject: [PATCH] Protecting access to the new Contacts API with a try/catch block. --- core/java/android/provider/ContactsContract.java | 34 +++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/core/java/android/provider/ContactsContract.java b/core/java/android/provider/ContactsContract.java index 59320400d533..18b666cb74a5 100644 --- a/core/java/android/provider/ContactsContract.java +++ b/core/java/android/provider/ContactsContract.java @@ -312,23 +312,30 @@ public final class ContactsContract { * @param contactUri the contact whose photo should be used */ public static Uri getPhotoUri(ContentResolver cr, Uri contactUri) { - long photoId = -1; - Cursor cursor = cr.query(contactUri, new String[]{Contacts.PHOTO_ID}, null, null, null); + + // TODO remove try/catch block as soon as eclair-dev is merged in eclair try { - if (!cursor.moveToNext()) { - return null; - } + long photoId = -1; + Cursor cursor = cr.query(contactUri, new String[] {Contacts.PHOTO_ID}, + null, null, null); + try { + if (!cursor.moveToNext()) { + return null; + } - if (cursor.isNull(0)) { - return null; + if (cursor.isNull(0)) { + return null; + } + + photoId = cursor.getLong(0); + } finally { + cursor.close(); } - photoId = cursor.getLong(0); - } finally { - cursor.close(); + return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId); + } catch (Exception e) { + return null; } - - return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId); } /** @@ -339,6 +346,9 @@ public final class ContactsContract { */ public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri) { Uri photoUri = getPhotoUri(cr, contactUri); + if (photoUri == null) { + return null; + } Cursor cursor = cr.query(photoUri, new String[]{ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null); try { -- 2.11.0