i'm new to developing android apps and I'm currently trying to do the following: Search all contacts' notes for a certain string (say: "E-Plus") and, if found, place a certain icon (say the E-Plus logo) as their contact photo.
我是开发Android应用程序的新手,我正在尝试执行以下操作:搜索所有联系人的笔记以查找某个字符串(例如:“E-Plus”),如果找到,则放置一个特定的图标(例如E -Plus logo)作为他们的联系人照片。
I got it working using eclipse and the following test AVD: Platform: Android 2.3.3 API Level: 10 CPU/ABI: ARM (armeabi)
我使用eclipse和以下测试AVD工作:平台:Android 2.3.3 API级别:10 CPU / ABI:ARM(armeabi)
Now, when i copy the *.apk file generated from eclipse to my Samsung Galaxy S2, the app only works partly. I added a line to display a toast after searching all contacts, that tells me how many contacts have been found with the specific string. This number is correct, when i execute the app on my actual Galaxy S2.
现在,当我将从eclipse生成的* .apk文件复制到我的三星Galaxy S2时,该应用程序只能部分工作。我在搜索所有联系人后添加了一行显示祝酒词,告诉我在特定字符串中找到了多少联系人。当我在我的实际Galaxy S2上执行应用程序时,这个数字是正确的。
But in contrary to the testing environment, the app doesnt set the photo afterwards. The app also doesn't crash. Then i thought, setting a photo might be too difficult as a first approach, so i also tried adding a phone number - same result. The phone number is added correctly in the testing environment, but not on my actual phone.
但与测试环境相反,应用程序之后不会设置照片。该应用程序也不会崩溃。然后我想,设置一张照片作为第一种方法可能太难了,所以我也尝试添加一个电话号码 - 相同的结果。电话号码在测试环境中正确添加,但不在我的实际手机上。
I tried both a google contact on my phone and a normal phonebook entry contact to have the certain note in its details. Both were found correctly by the app (that is note was read and string was found) but neither of them had the photo or phone number set after running the app.
我尝试了手机上的谷歌联系人和普通的电话簿入口联系人,以便在其详细信息中加入特定说明。两者都被应用程序正确找到(即已读取注释并找到字符串),但在运行应用程序后,他们都没有设置照片或电话号码。
Here is what i do:
这是我做的:
The following code should load all contacts and save their id and note. Note that ContactInfo is just a simple class with variables to hold the contactId etc. (String contactId, String note, Byte[] Photo) and according get and set methods.
以下代码应加载所有联系人并保存其ID和注释。请注意,ContactInfo只是一个简单的类,其中包含用于保存contactId等的变量(String contactId,String note,Byte [] Photo)以及get和set方法。
public ArrayList loadAllContacts(ContentResolver cr) {
ArrayList cOntactList= new ArrayList();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String cOntactId= cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
ContactInfo cOntactInfo= new ContactInfo(contactId);
// Get Note
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] whereParams = new String[]{contactId,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
contactInfo.setNote(note);
}
noteCur.close();
contactList.add(contactInfo);
}
}
return contactList;
}
The following code should update all contacts setting a photo (and for further testing, a phone number). The setPhoto method I got from the following blog: Handling Contact Photos and because it worked perfectly in the testing AVD I thought that theres probably no error in it.
以下代码应更新设置照片的所有联系人(以及进一步测试,电话号码)。我从以下博客获得的setPhoto方法:处理联系人照片,因为它在测试AVD中完美运行,我认为它可能没有错误。
public void updateAllContacts(ContentResolver cr, ArrayList contactList) {
while (!contactList.isEmpty()) {
ContactInfo cOntact= contactList.remove(0);
String cOntactId= contact.getContactId();
setPhoto(cr,contact.getPhoto(),contactId);
setPhoneNumber(cr,contact.getPhoneNumber(),contactId);
}
}
public void setPhoneNumber(ContentResolver cr, String phoneNumber, String contactId) {
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,phoneNumber);
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_CUSTOM);
cr.insert(ContactsContract.Data.CONTENT_URI, values);
}
public void setPhoto(ContentResolver cr, byte[] bytes, String contactId) {
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + contactId + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if (cursor.moveToFirst()) {
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
cr.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
} else {
cr.insert(ContactsContract.Data.CONTENT_URI, values);
}
}
The following routine I use to get the byte array from the *.png in the res folder of my app.
我使用以下例程从我的应用程序的res文件夹中的* .png获取字节数组。
public static byte[] getByteArrayFromResource(Resources rsrc,int id) {
Bitmap bmp = BitmapFactory.decodeResource(rsrc,id);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
return bytes;
}
Finally, what I execute in my main routine, is:
最后,我在主程序中执行的是:
ArrayList cOntactList= new ArrayList();
ContactsAccessor cOntactsAccessor= new ContactsAccessor();
cOntactList= contactsAccessor.loadAllContacts(getContentResolver());
int test = 0;
for (int i=0; i0) Toast.makeText(WelchesNetzIcons.this,"Es wurden "+test+" Icons gesetzt.", Toast.LENGTH_SHORT).show();
else Toast.makeText(WelchesNetzIcons.this, "Es wurden keine Icons gesetzt.", Toast.LENGTH_SHORT).show();
I geatly appreciate any suggestions on what I might be doing wrong here. Also, I tried to be as precise as I could, but if you are interested in or need any further information, please don't hesitate to ask.
我非常感谢任何有关我在这里做错的建议。此外,我尽可能精确,但如果您感兴趣或需要任何进一步的信息,请不要犹豫。
Thank you very much for your effort in helping me.
非常感谢你帮助我的努力。
Best regards, Niklas
最好的问候,尼克拉斯
1
Try to use this code. It works for me.
尝试使用此代码。这个对我有用。
Your problem may be in wrong contact id or in wrong mimetype
您的问题可能是错误的联系人ID或错误的mimetype
//looking for contact
//寻找联系方式
String select = String.format("%s=? AND %s='%s'", Phone.NUMBER, Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
String[] project = new String[] { Data.RAW_CONTACT_ID };
Cursor c = context.getContentResolver().query(Data.CONTENT_URI, project, select, new String[] { contact.getPhone() }, null);
if(c.moveToFirst()){
rawCOntactId= c.getLong(c.getColumnIndex(Data.RAW_CONTACT_ID));
}
//set photo
Bitmap bit = //your photo
ByteArrayOutputStream streamy = new ByteArrayOutputStream();
bit.compress(CompressFormat.PNG, 0, streamy);
byte[] photo = streamy.toByteArray();
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
ContactHelper.context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);