2012-01-30 95 views
1

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

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"); 
     } 
    } 
+0

http://www.droidnova.com/add-contact-photo-to-your-list-application,45.html – Andy 2012-01-30 04:22:00

+0

使用过时的方法 – 2012-01-30 04:26:39

+0

我的目标是2.2 – 2012-01-30 04:27:07

回答

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

+0

嗯,你确定这是有效的?我应该把它放在哪里,因为我不认为它有效。 – 2012-02-16 23:16:41

+0

你可以在哪里显示联系人图片。使用位图照片并设置你想要的位置 – Sameer 2012-02-17 04:15:04