簡體   English   中英

如何從photo_id獲取photo_uri

[英]how to get photo_uri from photo_id

我瀏覽了通訊錄,嘗試獲取所有聯系人的不為空的照片。

我使用的是Android API8,因此無法查詢image_uri。

給定photo_id(API 5),我如何獲取照片位圖?

這是我的查詢:

public final static String[] PROJECTION2 = new String[] {
        ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.PHOTO_ID
};

 public static void fillAddressBookData() {
    String sHash = null;
    String where = ContactsContract.Contacts.IN_VISIBLE_GROUP + "= ? ";
    String[] selectionArgs = new String[] {"1"};
    Cursor cursor =
        AppService
            .getAppContext()
            .getContentResolver()
            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION2, where,
                selectionArgs, null);

...

嘗試這個:

private void GetImageByPhoneNumber(String number)
    {
        Uri uri1 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));
        Cursor test = getContentResolver().query(uri1,
                new String[] { "photo_uri" }, null, null,
                null);

        if (test.getCount() > 0) {
            test.moveToFirst();

            Uri photoUri = Uri.parse(test.getString(test
                    .getColumnIndexOrThrow("photo_uri"))));

            Bitmap image = getPhoto(context , photoUri);


        }

        test.close();
    }

並獲取照片:

public static Bitmap getPhoto(Context context , Uri uri){

        Bitmap bm = null;
        try {
            bm = BitmapFactory.decodeStream(context.getContentResolver()
                    .openInputStream(uri));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if (bm == null) {
            bm = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.default_user_avatar);
        }
        return bm;
    }

嘗試下一個代碼片段,它應該可以解決問題

public Uri getPhotoUri(long contactId) {
            ContentResolver contentResolver = getContentResolver();

            try {
                Cursor cursor = contentResolver
                        .query(ContactsContract.Data.CONTENT_URI,
                                null,
                                ContactsContract.Data.CONTACT_ID
                                        + "="
                                        + contactId
                                        + " AND "

                                        + ContactsContract.Data.MIMETYPE
                                        + "='"
                                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                        + "'", null, null);

                if (cursor != null) {
                    if (!cursor.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, contactId);
            return Uri.withAppendedPath(person,
                    ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM