如何使用联系人ID检索联系人图片

时间:2012-07-07 18:17:26

标签: android android-contacts

我想要一个接收id(long)并返回相应联系人pic(Bitmap或InputStream)的函数 尝试了很多。但是我无法将其拉下来。

PS - 最低API等级= 10

3 个答案:

答案 0 :(得分: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;         

}

希望这会有所帮助。

答案 1 :(得分: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();

答案 2 :(得分: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;

}

这是我的代码。它正在发挥作用。