2012-07-07 152 views
0

我想要一個函數,它需要一個聯繫人ID(長)並返回相應的聯繫人圖片(位圖或InputStream) 已經嘗試了很多。但我無法將其取消。如何使用聯繫人ID檢索聯繫人圖片

PS - 最小API等級= 10

+0

你試過了嗎?張貼你的代碼.. – 2012-07-07 18:23:18

回答

0

在你的活動,你需要的網址下載圖片。使用以下方法(請確保此代碼必須在該活動中,您要下載的圖片):

private class DownloadProfilePicture extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 



        InputStream in = null; 
        int response = -1; 

        URL url = "your image url"; 
        URLConnection conn = null; 
        HttpURLConnection httpConn = null; 

        conn = url.openConnection(); 

        if (!(conn instanceof HttpURLConnection)) 
         throw new IOException("Not an HTTP connection"); 

        httpConn = (HttpURLConnection) conn; 
        httpConn.setAllowUserInteraction(false); 
        httpConn.setInstanceFollowRedirects(true); 
        httpConn.setRequestMethod("GET"); 
        httpConn.connect(); 

        response = httpConn.getResponseCode(); 
        if (response == HttpURLConnection.HTTP_OK) { 
         in = httpConn.getInputStream(); 
        } 

        if (in != null && in.available() > 807) { 
         yourBitmaptype.setBitmap(
           BitmapFactory.decodeStream(in)); 

        } else { 
         users.get(screenName).setBitmap(
           BitmapFactory.decodeResource(getResources(), 
             R.drawable.default_profile_pic)); 

        } 
        in.close(); 
        in = null; 

      } catch (Exception e) { 
       users.get(temp).setBitmap(
         BitmapFactory.decodeResource(getResources(), 
           R.drawable.default_profile_pic)); 
       Log.e(TAG, "Downloading Image Exception.. Using default"); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

      // use post execute logic 
      super.onPostExecute(result); 
     } 

     @Override 
     protected void onPreExecute() { 
     // use pre execute logic 
      super.onPreExecute(); 
     } 

    } 

onCreate()稱呼其爲new DownloadProfilePicture().execute();

+0

謝謝!我想我找到了一個更好的方法。 – rockydgeekgod 2012-07-08 05:17:09

+0

如何獲取上次插入的聯繫人ID。請給我那個代碼 – AndroidRaji 2012-12-20 12:24:36

3

試試下面的代碼:

private void setContactInfo(long id){ 
Bitmap photoBitmap = null; 
Uri contactUri = ContentUris.withAppendedId(
      ContactsContract.Contacts.CONTENT_URI, id); 

Cursor cursor = managedQuery(contactUri, null, null, null, null); 
    cursor.moveToFirst(); 
    contact_text.setText(cursor.getString(cursor 
      .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));//contact.text is a textView used to displays the contact name 

    String id = getIntent().getData().getLastPathSegment(); 
    // Photo cursor 

    String photoWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " 
      + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] photoWhereParams = new String[] { id, 
      ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE }; 
    Cursor photoCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, 
      photoWhere, photoWhereParams, null); 
    photoCur.moveToFirst(); 
    if (photoCur.moveToFirst() && photoCur != null) { 

     byte[] photoBlob = photoCur.getBlob(photoCur 
       .getColumnIndex(Photo.PHOTO)); 
     if (photoBlob != null) { 
      photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, 
        photoBlob.length); 

      contact_image.setImageBitmap(photoBitmap);//contact_image is an ImageView 
     } else { 
      photoBitmap = BitmapFactory.decodeResource(getResources(), 
        android.R.drawable.ic_menu_report_image);//android.R.drawable.ic_menu_report_image is the default image if a Contact doesn't have any image stored 
      contact_image.setImageBitmap(photoBitmap); 
     } 

    } 
cursor.close; 
photoCur.close;   

} 

希望這會有所幫助。

+0

謝謝!我想我找到了一個更好的方法。 – rockydgeekgod 2012-07-08 05:09:31

+0

如果您觀察我發佈的代碼與您的代碼相同,並且我還處理了聯繫人圖片不可用的情況。 – 2012-07-08 06:30:18

+0

是的,它給出了一個默認圖片,不是嗎? – rockydgeekgod 2012-07-08 07:12:09

0
private ByteArrayInputStream getPhoto() 
{ 

    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);  
    Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);  
    Cursor cursor = getContentResolver().query(photoUri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);  
    if (cursor == null) {   
     return null;  
    }  
    try 
    {   
     if (cursor.moveToFirst()) 
     {    
      byte[] data = cursor.getBlob(0);    
      if (data != null) 
      {     
       return new ByteArrayInputStream(data);    
      }   
     }  
    } 
    finally 
    {   
     cursor.close();  
    }  
    return null; 

} 

這是我的代碼。這是工作。