在android中获取联系人照片给出null

问题描述:

我想在用户输入号码时获取联系人的照片。通过使用电话号码,我获取用户名,但对于图像,它显示为空。在android中获取联系人照片给出null

我的代码如下:

public class NewtempActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final ImageView img = (ImageView) findViewById(R.id.imageView1); 
     final EditText edit = (EditText) findViewById(R.id.editText1); 
     TextView txt = (TextView) findViewById(R.id.textView1); 
     Button btn = (Button) findViewById(R.id.button1); 

     btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("Girish", "Clicked"); 

       String name = getContactNameFromNumber(edit.getText() 
         .toString(), getApplicationContext()); 
       img.setImageBitmap(BitmapFactory 
         .decodeFile(ContactsContract.PhoneLookup._ID)); 
       Log.d("Girish", 
         "" 
           + (BitmapFactory 
             .decodeFile(ContactsContract.PhoneLookup._ID))); 
       Toast.makeText(getApplicationContext(), name, name.length()) 
         .show(); 
      } 
     }); 
    } 

    public String getContactNameFromNumber(String number, Context ctx) { 
     /* 
     * // define the columns I want the query to return String[] projection 
     * = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, 
     * ContactsContract.PhoneLookup.NUMBER, }; 
     */ 
     // encode the phone number and build the filter URI 
     Uri contactUri = Uri.withAppendedPath(
       ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
       Uri.encode(number)); 

     // query time 
     // Cursor c = ctx.getContentResolver().query(contactUri, projection, 
     // null, 
     Cursor c = ctx.getContentResolver().query(contactUri, null, null, null, 
       null); 

     // if the query returns 1 or more results 
     // return the first result 
     if (c.moveToFirst()) { 
      String name = c.getString(c 
        .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 

      return name; 
     } 

     // return the original number if no match was found 
     return number; 
    } 

    public static Bitmap loadContactPhoto(ContentResolver cr, long id) { 
     Uri uri = ContentUris.withAppendedId(
       ContactsContract.Contacts.CONTENT_URI, id); 
     InputStream input = ContactsContract.Contacts 
       .openContactPhotoInputStream(cr, uri); 
     // InputStream input = ContactsContract.Contacts.Photo 
     if (input == null) { 
      return null; 
     } 
     return BitmapFactory.decodeStream(input); 
    } 

    public InputStream openPhoto(long contactId) { 
     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 
       contactId); 
     Uri photoUri = Uri.withAppendedPath(contactUri, 
       Contacts.Photo.CONTENT_DIRECTORY); 
     Cursor cursor = getContentResolver().query(photoUri, null, 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; 
    } 

} 

请建议我在那里通过你的代码会做wrong.I增加了读取联系*限也

我才知道像你正在尝试从号码中获取联系人姓名。并使用你想要的联系人图片..但你永远不会叫你的功能,你为联系图片.. :) ..所以你可以做的是从联系人号码id,并在该id拍照。所以你会得到的数字的照片..

package com.android.SampleProject; 

import java.io.ByteArrayInputStream; 
import java.io.InputStream; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.ContentUris; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.Contacts; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class NewtempActivity extends Activity { 

    private long id; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final ImageView img = (ImageView) findViewById(R.id.imageView1); 
     final EditText edit = (EditText) findViewById(R.id.editText1); 
     Button btn = (Button) findViewById(R.id.button1); 


     btn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       Log.d("Girish", "Clicked"); 

       String name = getContactNameFromNumber(edit.getText() 
         .toString(), getApplicationContext()); 
       img.setImageBitmap(BitmapFactory 
         .decodeFile(ContactsContract.PhoneLookup._ID)); 
       img.setImageBitmap(loadContactPhoto(getContentResolver(), id)); 
       Log.d("Girish", 
         "" 
           + (BitmapFactory 
             .decodeFile(ContactsContract.PhoneLookup._ID))); 
       Toast.makeText(getApplicationContext(), name, name.length()) 
         .show(); 
      } 
     }); 
    } 

    public String getContactNameFromNumber(String number, Context ctx) { 
     /* 
     * // define the columns I want the query to return String[] projection 
     * = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, 
     * ContactsContract.PhoneLookup.NUMBER, }; 
     */ 
     // encode the phone number and build the filter URI 
     Uri contactUri = Uri.withAppendedPath(
       ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
       Uri.encode(number)); 

     // query time 
     // Cursor c = ctx.getContentResolver().query(contactUri, projection, 
     // null, 
     Cursor c = ctx.getContentResolver().query(contactUri, null, null, null, 
       null); 

     // if the query returns 1 or more results 
     // return the first result 
     if (c.moveToFirst()) { 
      String name = c.getString(c 
        .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      id = c.getLong(c 
        .getColumnIndex(ContactsContract.PhoneLookup._ID)); 
      return name; 
     } 

     // return the original number if no match was found 
     return number; 
    } 

    public static Bitmap loadContactPhoto(ContentResolver cr, long id) { 
     Uri uri = ContentUris.withAppendedId(
       ContactsContract.Contacts.CONTENT_URI, id); 
     InputStream input = ContactsContract.Contacts 
       .openContactPhotoInputStream(cr, uri); 
     // InputStream input = ContactsContract.Contacts.Photo 
     if (input == null) { 
      return null; 
     } 
     return BitmapFactory.decodeStream(input); 
    } 

    public InputStream openPhoto(long contactId) { 
     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 
       contactId); 
     Uri photoUri = Uri.withAppendedPath(contactUri, 
       Contacts.Photo.CONTENT_DIRECTORY); 
     Cursor cursor = getContentResolver().query(photoUri, null, 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; 
    } 

} 
+0

是的,这是我的错误... – Shruti 2011-12-26 10:51:13

+0

请接受答如果你有答案 – 2011-12-26 10:53:36