0

我想要一个函数,它接受一个联系人 id(long) 并返回相应的联系人 pic(Bitmap 或 InputStream) 已经尝试了很多。但我无法做到这一点。

PS - 最低 API 级别 = 10

4

3 回答 3

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;         

}

希望这可以帮助。

于 2012-07-07T19:18:28.087 回答
0

在您的活动中,您需要下载图片的url 。使用以下方法(确保此代码必须在您要下载图片的那个活动中):

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()并从as 中调用它new DownloadProfilePicture().execute();

于 2012-07-07T18:33:26.997 回答
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;

}

这是我的代码。这是工作。

于 2012-07-08T05:18:04.220 回答