0

嗨,我正在尝试从联系人获取图像,但此方法不适用于 2.3.6 的 Galaxy S。是否有适用于所有电话的功能?在模拟器上工作正常。这是我的代码

 String idContact = fetchContactIdFromPhoneNumber(number, paramContext);

        if (idContact.compareTo("") != 0 )  {
            Log.i("Valore ",idContact);
              Bitmap bit= loadContactPhoto(paramContext.getContentResolver(),Long.parseLong(idContact));
              sms_rubrica.add(0,bit);
          }
         else  sms_rubrica.add(0,No_Foto);

public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
            Uri uri =    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
           InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);

            if (input == null) {
                return null;
            }
            return BitmapFactory.decodeStream(input);
    }

使您的功能适应我的小部件,它会引发 sql 异常

 public Uri getPhotoUri(String id,Context ctx) {
        try {
            Cursor cur = ctx.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID + "=" + id + " AND "
                            + ContactsContract.Data.MIMETYPE + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                    null);
            if (cur != null) {
                if (!cur.moveToFirst()) {
                    return null; // no photo
                }
            } else {
                return null; // error in cursor process
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
                .parseLong(id));
        return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }

这是堆栈跟踪:

     02-06 13:17:43.684: W/System.err(240): android.database.sqlite.SQLiteException:   near    "AND": syntax error: , while compiling: SELECT data_version, contact_id, lookup, data12,   data11, data10, mimetype, data15, data14, data13, data_sync1, data_sync3, data_sync2, data_sync4, account_type, custom_ringtone, status_updates.status AS status, data1, data4, data5, data2, data3, data8, data9, group_sourceid, data6, account_name, data7, display_name, in_visible_group, contacts_status_updates.status_res_package AS contact_status_res_package, is_primary, contacts_status_updates.status_ts AS contact_status_ts, raw_contact_id, times_contacted, contacts_status_updates.status AS contact_status, status_updates.status_res_package AS status_res_package, status_updates.status_icon AS status_icon, contacts_status_updates.status_icon AS contact_status_icon, presence.mode AS mode, version, last_time_contacted, res_package, _id, status_updates.status_ts AS status_ts, dirty, is_super_primary, photo_id, send_to_voicemail, contacts_status_updates.status_label AS contact_status_label, status_updates.status_label AS status_label, starred, agg_presence.mode AS contact_presence, sourceid FROM view_data_restricted data LEFT OUTER JOIN agg_presence ON (agg_presence.presence_contact_id=contact_id) LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) LEFT OUTER JOIN presence ON (presence_data_id=data._id) LEFT OUTER JOIN status_updates ON (status_updates.status_update_data_id=data._id) WHERE (1) AND (contact_id= AND mimetype='vnd.android.cursor.item/photo')
  02-06 13:17:43.684: W/System.err(240):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
  02-06 13:17:43.684: W/System.err(240):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
 02-06 13:17:43.684: W/System.err(240):     at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
 02-06 13:17:43.684: W/System.err(240):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
02-06 13:17:43.684: W/System.err(240):  at android.content.ContentResolver.query(ContentResolver.java:202)
02-06 13:17:43.684: W/System.err(240):  at com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget.getPhotoUri(Randomnuberwidget.java:186)
  02-06 13:17:43.684: W/System.err(240):    at com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget.onUpdate(Randomnuberwidget.java:114)

02-06 13:17:43.684: W/System.err(240): 在 android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:61) 02-06 13:17:43.684: W/System.err(240) : 在 com.imrankhanandroid.HomeWidgetRandomNum.Randomnuberwidget.onReceive(Randomnuberwidget.java:244) 02-06 13:17:43.684: W/System.err(240): 在 android.app.ActivityThread.handleReceiver(ActivityThread.java:2637 ) 02-06 13:17:43.684: W/System.err(240): 在 android.app.ActivityThread.access$3100(ActivityThread.java:119) 02-06 13:17:43.684: W/System.err( 240): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 02-06 13:17:43.684: W/System.err(240): 在 android.os.Handler.dispatchMessage(Handler.java :99) 02-06 13:17:43.684: W/System.err(240): 在 android.os.Looper.loop(Looper.java:123) 02-06 13:17:43.694: W/System.err (240):在 android.app。ActivityThread.main(ActivityThread.java:4363) 02-06 13:17:43.694: W/System.err(240): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-06 13:17:43.694 : W/System.err(240): 在 java.lang.reflect.Method.invoke(Method.java:521) 02-06 13:17:43.694: W/System.err(240): 在 com.android。 internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 02-06 13:17:43.694: W/System.err(240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :618) 02-06 13:17:43.694: W/System.err(240): 在 dalvik.system.NativeStart.main(本机方法)错误(240):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)02-06 13:17:43.694:W/System.err(240):在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:618) 02-06 13:17:43.694: W/System.err(240): at dalvik.system.NativeStart.main(Native Method)错误(240):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)02-06 13:17:43.694:W/System.err(240):在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:618) 02-06 13:17:43.694: W/System.err(240): at dalvik.system.NativeStart.main(Native Method)

4

1 回答 1

0

可能这会对您有所帮助(联系人由 getId() 识别):

/**
 * @return the photo URI
 */
public Uri getPhotoUri() {
    try {
        Cursor cur = this.ctx.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
                        + ContactsContract.Data.MIMETYPE + "='"
                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
            .parseLong(getId()));
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

用法是:

Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}
于 2013-02-06T11:52:14.557 回答