我的代码如下:
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); // 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); 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; }}
请建议我在哪里做错了.我还添加了阅读联系人权限
解决方法 通过你的代码我知道你正试图从号码中获取联系人姓名.并使用你想要的联系人图像..但你从来没有调用你为联系人图片所做的功能.. 总结以上是内存溢出为你收集整理的在android中获取联系人照片给出null全部内容,希望文章能够帮你解决在android中获取联系人照片给出null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)