在图像视图中显示联系人图像

时间:2012-01-30 04:08:32

标签: android uiimageview sms android-contacts

我想获取联系人图片并将其显示在imageview中。我希望这可以根据用户选择的联系方式工作。我有一个按钮,用户按下它并获取联系电话号码并显示在文本字段中。但现在我需要它来拍照并显示它。有没有人知道如何操纵这个来拍照? (忽略说电子邮件的日志和我说的电子邮件的变量,我操纵了代码,以便获取电话号码。)

case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String email = "";
                try {
                    Bundle extras = data.getExtras();
                    Set<String> keys = extras.keySet();
                    Iterator<String> iterate = keys.iterator();
                    while (iterate.hasNext()) {
                        String key = iterate.next();
                        Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
                    }

                    Uri result = data.getData();
                    Log.v(DEBUG_TAG,
                            "Got a contact result: " + result.toString());

                    // get the contact id from the Uri
                    String id = result.getLastPathSegment();

                    // query for everything email
                    cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone.CONTACT_ID + "=?", new String[] { id },
                            null);

                    int emailIdx = cursor.getColumnIndex(Phone.DATA);

                    // let's just get the first email
                    if (cursor.moveToFirst()) {

                        /*
                         * Iterate all columns. :) String columns[] =
                         * cursor.getColumnNames(); for (String column :
                         * columns) { int index = cursor.getColumnIndex(column);
                         * Log.v(DEBUG_TAG, "Column: " + column + " == [" +
                         * cursor.getString(index) + "]"); }
                         */

                        email = cursor.getString(emailIdx);

                        Log.v(DEBUG_TAG, "Got email: " + email);

                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to get email data", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                    EditText emailEntry = (EditText) findViewById(R.id.txtPhoneNo);
                    emailEntry.setText(email);
                    if (email.length() == 0) {
                        Toast.makeText(this, "No email found for contact.",
                                Toast.LENGTH_LONG).show();
                    }

                }

                break;
            }

        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }
    }

2 个答案:

答案 0 :(得分:0)

使用它

 Uri conUri=Uri.parse(url);
        InputStream photoInputStream=Contacts.openContactPhotoInputStream(mContext.getContentResolver(), conUri);
        if(photoInputStream==null)
            return framePhoto(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_contact_list_picture));
        Bitmap photo=framePhoto(getPhoto(mContext.getContentResolver(), conUri));

有关更多信息,请转到此问题

Load contact photo in a listview performance

答案 1 :(得分:0)

http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html

中查看openContactPhotoInputStream(android.content.ContentResolver,android.net.Uri)