Protecting access to the new Contacts API with a try/catch block.

This commit is contained in:
Dmitri Plotnikov
2009-08-20 08:13:46 -07:00
parent 41b379da57
commit 1c1629da30

View File

@@ -312,23 +312,30 @@ public final class ContactsContract {
* @param contactUri the contact whose photo should be used * @param contactUri the contact whose photo should be used
*/ */
public static Uri getPhotoUri(ContentResolver cr, Uri contactUri) { 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 { try {
if (!cursor.moveToNext()) { long photoId = -1;
return null; 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;
}
photoId = cursor.getLong(0);
} finally {
cursor.close();
} }
if (cursor.isNull(0)) { return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId);
return null; } catch (Exception e) {
} return null;
photoId = cursor.getLong(0);
} finally {
cursor.close();
} }
return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId);
} }
/** /**
@@ -339,6 +346,9 @@ public final class ContactsContract {
*/ */
public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri) { public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri) {
Uri photoUri = getPhotoUri(cr, contactUri); Uri photoUri = getPhotoUri(cr, contactUri);
if (photoUri == null) {
return null;
}
Cursor cursor = cr.query(photoUri, Cursor cursor = cr.query(photoUri,
new String[]{ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null); new String[]{ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
try { try {