Android 如何使用联系人id检索联系人图片

Android 如何使用联系人id检索联系人图片,android,android-contacts,Android,Android Contacts,我想要一个函数,它接收联系人id(long)并返回相应的联系人pic(位图或InputStream) 我试了很多。但我没能成功 PS-Min API Level=10在您的活动中,您需要url来下载图片。使用以下方法(确保此代码必须位于您要下载图片的活动中): 私有类下载ProfilePicture扩展异步任务{ @凌驾 受保护的Void doInBackground(Void…参数){ 试一试{ InputStream in=null; int响应=-1; URL=“您的图像URL”; URL

我想要一个函数,它接收联系人id(long)并返回相应的联系人pic(位图或InputStream) 我试了很多。但我没能成功


PS-Min API Level=10

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

私有类下载ProfilePicture扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
InputStream in=null;
int响应=-1;
URL=“您的图像URL”;
URLConnection conn=null;
HttpURLConnection httpConn=null;
conn=url.openConnection();
if(!(HttpURLConnection的连接实例))
抛出新IOException(“非HTTP连接”);
httpConn=(HttpURLConnection)conn;
httpConn.setAllowUserInteraction(假);
httpConn.setInstanceFollowRedirects(真);
httpConn.setRequestMethod(“GET”);
httpConn.connect();
response=httpConn.getResponseCode();
if(response==HttpURLConnection.HTTP\u OK){
in=httpConn.getInputStream();
}
if(in!=null&&in.available()>807){
yourBitmaptype.setBitmap(
BitmapFactory.decodeStream(in));
}否则{
users.get(屏幕名).setBitmap(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
}
in.close();
in=null;
}捕获(例外e){
users.get(temp.setBitmap)(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
Log.e(标记“下载图像异常..使用默认值”);
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//使用后执行逻辑
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//使用预执行逻辑
super.onPreExecute();
}
}

并将其从
onCreate()
调用为
newdownloadprofilepicture().execute()

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

私有类下载ProfilePicture扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
InputStream in=null;
int响应=-1;
URL=“您的图像URL”;
URLConnection conn=null;
HttpURLConnection httpConn=null;
conn=url.openConnection();
if(!(HttpURLConnection的连接实例))
抛出新IOException(“非HTTP连接”);
httpConn=(HttpURLConnection)conn;
httpConn.setAllowUserInteraction(假);
httpConn.setInstanceFollowRedirects(真);
httpConn.setRequestMethod(“GET”);
httpConn.connect();
response=httpConn.getResponseCode();
if(response==HttpURLConnection.HTTP\u OK){
in=httpConn.getInputStream();
}
if(in!=null&&in.available()>807){
yourBitmaptype.setBitmap(
BitmapFactory.decodeStream(in));
}否则{
users.get(屏幕名).setBitmap(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
}
in.close();
in=null;
}捕获(例外e){
users.get(temp.setBitmap)(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_profile_pic));
Log.e(标记“下载图像异常..使用默认值”);
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//使用后执行逻辑
super.onPostExecute(结果);
}
@凌驾
受保护的void onPreExecute(){
//使用预执行逻辑
super.onPreExecute();
}
}

并将其从
onCreate()
调用为
newdownloadprofilepicture().execute()

请尝试以下代码:

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;         

}

希望这有帮助

请尝试以下代码:

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;         

}
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;

}
希望这有帮助

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;

}
这是我的密码。它正在工作


这是我的密码。它正在工作。

您尝试了什么?发布您的代码..您尝试了什么?发布您的代码..谢谢!我想我找到了一个更好的方法。如果你注意到我发布的代码与你的代码相同,并且我也注意到了联系人照片不可用的情况。是的,它给出了默认图片,不是吗?是的,但当我知道你在一小时前找到了解决方案,而事实上我在将近11小时前给了你时,我一点也不惊讶:)事实上我根本没有试过你的。我甚至都没看到。我是