In my code I update contact information including name, address, email, and photo. When contact have no photo all OK. But after assigning a photo for the contact I get android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string on each update operation.
My code for adding photo to contact
Bitmap bit =getBitmap();
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);
try {
if (getPhotoUri(rawContactId) != null) {
ContactHelper.context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values,
String.format("%s = ?", ContactsContract.Data.RAW_CONTACT_ID), new String[] {String.format("%d", rawContactId)});
} else {
ContactHelper.context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}
} catch (Exception ex) {
ex.printStackTrace();
}
My code to change contact name
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add( ContentProviderOperation.newUpdate( Data.CONTENT_URI )
.withSelection( Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] { String.valueOf( rawContactId ) } )
.withValue( ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getName() )
.build() );
try {
ContentResolver cr = context.getContentResolver();
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
There is a nice little blog entry on ThinkAndroid that will show you how to do this:
http://thinkandroid.wordpress.com/2009/12/30/handling-contact-photos-all-api-levels/
The core part is this:
public static void setContactPhoto(ContentResolver c, byte[] bytes, long personId) {
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + personId + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = c.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, personId);
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) {
c.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
} else {
c.insert(ContactsContract.Data.CONTENT_URI, values);
}
}
Basically, what you can’t just do is:
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes.toByteArray());
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE );
context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.RAW_CONTACT_ID + " = " + personId, null);
And that’s because there are many rows where the RAW_CONTACT_ID = personId (there are data rows for the photo, phone numbers, emails, etc) and so trying this should give you some kind of SQLite exception along the lines of “CANNOT CONVERT BLOB TO STRING” and this makes sense as in this case you’re probably trying to update the phone number row with these bytes and so it’s throwing the appropriate error. Thus, in the first example, you need to begin by identifying which of these rows where RAW_CONTACT_ID = personId contains the BLOB value of the photo, and in the second query you want to set it in that row specifically. Now notice that sometimes photoRow may come back as -1. This basically tells us that no one has tried to set the photo or any other information before, and so we simply need to do a getContentResolver.insert(). Now, exactly why the behavior is different in these two cases, I don’t know. But the code above works, so once Google releases some better documentation maybe we’ll find out!
Related
I want to get WhatsApp profile picture and number but using contentResolver I will getting only name and number using following snippet code.
private void showContactWhatsApp(){
ContentResolver cr = getContentResolver();
Cursor contactCursor = cr.query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID,
ContactsContract.RawContacts.CONTACT_ID},
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",
new String[]{"com.whatsapp"},
null);
ArrayList<String> myWhatsappContacts = new ArrayList<>();
if (contactCursor != null) {
if (contactCursor.getCount() > 0) {
if (contactCursor.moveToFirst()) {
do {
//whatsappContactId for get Number,Name,Id ect... from ContactsContract.CommonDataKinds.Phone
String whatsappContactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
if (whatsappContactId != null) {
//Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID
Cursor whatsAppContactCursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{whatsappContactId}, null);
if (whatsAppContactCursor != null) {
whatsAppContactCursor.moveToFirst();
String id = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
whatsAppContactCursor.close();
//Add Number to ArrayList
myWhatsappContacts.add(number);
Log.e(TAG, " WhatsApp contact id : " + id);
Log.e(TAG, " WhatsApp contact name : " + name);
Log.e(TAG, " WhatsApp contact number : " + number);
}
}
} while (contactCursor.moveToNext());
contactCursor.close();
}
}
}
Log.e(TAG, " WhatsApp contact size : " + myWhatsappContacts.size());
}
I want to get WhatsApp profile picture like SyncMe app.
I wait to get WhatsApp contact list with name, number, and thumbnail.
Firstly I just want to reiterate the difference between a RawContact and a Contact. The latter being an aggregation (representing a person) of the former (representing an account).
Depending on what use you have for the image, it may be better to fetch the aggregate Contact and use the Profile image selected there which can be achieved via the Photo contract.
Uri photoUri;
Cursor photoCur = cr.query(
ContactsContract.Data.CONTENT_URI, null,
ContactsContract.Data.CONTACT_ID + "=" + aggregateContactId + " AND " +
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'",
null, null
);
if (photoCur != null && photoCur.moveToFirst()) {
Uri photoId = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggregateContactId);
photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);
}
If you have use specifically for the WhatsApp photo, first make sure the photo is cached on the device (just to avoid false flags while testing - open the contacts full image in WhatsApp and you can be sure it's cached) then you'll have to do a separate lookup for the image (from API 14):
Uri photoId = ContentUris.withAppendedId(RawContacts.CONTENT_URI, whatsappContactId);
Uri photoUri = Uri.withAppendedPath(photoId, RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
Can anybody guide me to find the solution for the following problem.
I have to identify whether the phone contact saved in locally or from Email ?(programatically)
i have read fromgoogle doc that ContactsContract.Groups, which contains information about raw contact groups such as Gmail contact groups. The current API does not support the notion of groups spanning multiple accounts.
Based on that i have tried the following code.
StringBuffer output = new StringBuffer();
final String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID,
ContactsContract.Groups.TITLE,
ContactsContract.Groups.SUMMARY_WITH_PHONES
};
Cursor c = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
null, ContactsContract.Groups.TITLE);
int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);
output.append("title"+IDX_TITLE+"\n");
Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();
while (c.moveToNext()) {
output.append("test...\n");
GroupInfo g = new GroupInfo();
g.id = c.getString(IDX_ID);
g.title = c.getString(IDX_TITLE);
output.append("title"+c.getString(IDX_TITLE)+"\n");
int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
if (users>0) {
// group with duplicate name?
GroupInfo g2 = m.get(g.title);
if (g2==null) {
m.put(g.title, g);
output.append("title"+g.title+"\n");
groups.add(g);
} else {
g2.id+=","+g.id;
}
}
}
outputText.setText(output);
c.close();
but no hope.
I am posting this answer for future use. We can differentiate the local phone contacts and the sync contacts by using the field called RawContacts.SOURCE_ID
It is described here
SOURCE_ID
read/write
String that uniquely identifies this row to its source account. Typically it is set at the time the raw contact is inserted and never changed afterwards. The one notable exception is a new raw contact: it will have an account name and type (and possibly a data set), but no source id. This indicates to the sync adapter that a new contact needs to be created server-side and its ID stored in the corresponding SOURCE_ID field on the phone.
The sample code is follows, it gives the id for sync contacts and null for others.
private void testContact() {
StringBuffer output = new StringBuffer();
ContentResolver resolver = getContentResolver();
Cursor contacts = resolver.query(Contacts.CONTENT_URI, null,
Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID
+ " ASC");
Cursor data = resolver.query(Data.CONTENT_URI, null, Data.MIMETYPE
+ "=? OR " + Data.MIMETYPE + "=?", new String[]{
Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
Data.CONTACT_ID + " ASC");
int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
boolean hasData = data.moveToNext();
while (contacts.moveToNext()) {
long id = contacts.getLong(idIndex);
Uri rawContactUri =
ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);
Uri entityUri =
Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
Cursor c =
getContentResolver().query(
entityUri,
new String[] {
RawContacts.ACCOUNT_NAME,
RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
null, null, null);
try {
while (c.moveToNext()) {
String sourceId = c.getString(0);
if (!c.isNull(1)) {
String source_id = c.getString(1);
try {
output.append(c.getString(4)+sourceId+" "+source_id+"\n");
//output.append(datas+ "Sync1 "+ c.getString(4)+" Sync2 "+ c.getString(5)+" Sync3"+ c.getString(6)+" Sync4 "+ c.getString(7)+"\n");
} catch (Exception e) {
e.printStackTrace();
}
//decide here based on mimeType, see comment later
}
}
} finally {
c.close();
}
}
outputText.setText(output);
}
Have a read through this and see if ACCOUNT_TYPE might be able to help you
I have a pretty basic content provider that stores 4 columns in a sqlite db. These columns are an auto incrementing key, a url as text, a long, and a blob for an image. When i try and query off the url, i get no results, even though i know the row exists in the database. What do I need to do with/to the url in order for the selection to pull it.
//called when storing
private void addKey(String url, final Bitmap bm) {
ContentValues values = new ContentValues();
values.put(ImagesContentProvider.URL, "'" + url + "'");
values.put(ImagesContentProvider.TIME, System.currentTimeMillis());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
values.put(ImagesContentProvider.BITMAP, bos.toByteArray());
cr.insert(ImagesContentProvider.CONTENT_URI, values); //cr is content resolver
}
//called for querying
private void query(String url) {
String selection = ImagesContentProvider.URL + "='" + url+ "'";
Cursor resultsCursor = cr.query(ImagesContentProvider.CONTENT_URI,
null, selection, null, null);
if (!resultsCursor.moveToFirst()) {
Log.d(TAG, "Not found in DB");
resultsCursor.close();
} else {
Log.e(TAG, "found in DB");
resultsCursor.close();
}
}
i know that you already know the answer but ...
error was here
values.put(ImagesContentProvider.URL, "'" + url + "'");
this should be:
values.put(ImagesContentProvider.URL, url);
//or you should add ' when you do query
you unecessary escape string in ContentValues ...
to prevent such errors do not escape string by yorself ... use parametrized queries
Cursor resultsCursor = cr.query(ImagesContentProvider.CONTENT_URI,
null, ImagesContentProvider.URL + "=?", new String[] { url }, null);
I'm still very new to android app development, and I have hit a problem I hope u can help me with...
I am trying to retrieve any "Notes" stored against a contact within my phone. I want to check if a contact (current caller) has any notes associated to them, and then either display the contents or do some action depending on the content of them etc...
I have tried the code below as a test to see if the data is retrieved anywhere within my cursor, but although it retrieves some data, I can't see the content of a note - so I guess I'm in the wrong place!
Where I have declared contentID, this is a result of doing a lookup with the code below, and using the id that is recieved.
Uri lookupUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(inCommingNumber));
String[] mPhoneNumberProjection = { Phone._ID, Phone.NUMBER, Phone.DISPLAY_NAME};
Cursor cur = getContentResolver().query(lookupUri , mPhoneNumberProjection, null, null, null);
private boolean hasNote(String contactID){
Boolean noteFound = false;
Cursor noteCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
if(noteCur.moveToFirst()) {
int numCols = noteCur.getColumnCount();
if(numCols > 0){
for(int x = 0; x < numCols; x++){
Log.d(APP_TAG, " column " + x + " contains: " + noteCur.getString(x));
}
}
noteFound = true;
} else{
Log.d(APP_TAG, "No Note retrieved");
}
noteCur.close();
return noteFound;
}
Apologies, I cant seem to get the code to display properly in this post!!
Any help would be great
I have tried various things, but can't seem to be able to get this data. I have created the note by simply adding it through the normal contact manager. I'm using Android 2.2.
The following code would display the first note for all contacts on your phone:
Cursor contactsCursor = null;
try {
contactsCursor = getContentResolver().query(RawContacts.CONTENT_URI,
new String [] { RawContacts._ID },
null, null, null);
if (contactsCursor != null && contactsCursor.moveToFirst()) {
do {
String rawContactId = contactsCursor.getString(0);
Cursor noteCursor = null;
try {
noteCursor = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Note.NOTE},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'",
new String[] {rawContactId}, null);
if (noteCursor != null && noteCursor.moveToFirst()) {
String note = noteCursor.getString(noteCursor.getColumnIndex(Note.NOTE));
Log.d(APP_TAG, "Note: " + note);
}
} finally {
if (noteCursor != null) {
noteCursor.close();
}
}
} while (contactsCursor.moveToNext());
}
} finally {
if (contactsCursor != null) {
contactsCursor.close();
}
}
If you are storing some sort of structured data on the note to take actions on using the free form note field may not be the best approach. With the new contacts contract model you are able to attach your own data fields (in ContactsContract.Data) to a contact just give them your own unique mimetype.
Alright, I'm a little new to the Android SDK, so forgive me if my question doesn't make sense or is very trivial. I'd like to add a custom field for contacts, that contains the contacts username on a website I'm doing this app for. And, with this custom field, I'd like to have the ability to click it (like "Send message" or "Call mobile") so that I can go to a specif Activity in my application, with a TextView set with the username that I just clicked on.
Sorry if that is a bit confusing, if you need anything else let me know!
It's working! But I changed Data.CONTACT_ID to Data.RAW_CONTACT_ID here:
if (mod == 0) {
values.put(Data.CONTACT_ID, this.getId());
values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
ctx.getContentResolver().insert(Data.CONTENT_URI, values);
}
You have to creat your own mime type for those.
Here is an example that saves a boolean as my custom mime type to the contacts. It uses the latest SDK 2.1
public static final String MIMETYPE_FORMALITY = "vnd.android.cursor.item/useformality";
public clsMyClass saveFormality() {
try {
ContentValues values = new ContentValues();
values.put(Data.DATA1, this.getFormality() ? "1" : "0");
int mod = ctx.getContentResolver().update(
Data.CONTENT_URI,
values,
Data.CONTACT_ID + "=" + this.getId() + " AND "
+ Data.MIMETYPE + "= '"
+ clsContacts.FORMALITY_MIMETYPE + "'", null);
if (mod == 0) {
values.put(Data.CONTACT_ID, this.getId());
values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
ctx.getContentResolver().insert(Data.CONTENT_URI, values);
}
} catch (Exception e) {
Log.v(TAG(), "saveFormality failed");
}
return this;
}
public boolean getFormality() {
if (data.containsKey(FORMALITY)) {
return data.getAsBoolean(FORMALITY);
} else {
// read formality
Cursor c = readDataWithMimeType(clsContacts.MIMETYPE_FORMALITY, this.getId());
if (c != null) {
try {
if (c.moveToFirst()) {
this.setFormality(c.getInt(0) == 1);
return (c.getInt(0) == 1);
}
} finally {
c.close();
}
}
return false;
}
}
public clsMyClass setFormality(Boolean value) {
data.remove(FORMALITY);
data.put(FORMALITY, value);
return this;
}
/**
* Utility method to read data with mime type
*
* #param mimetype String representation of the mimetype used for this type
* of data
* #param contactid String representation of the contact id
* #return
*/
private Cursor readDataWithMimeType(String mimetype, String contactid) {
return ctx.getContentResolver().query(
Data.CONTENT_URI,
new String[] {
Data.DATA1
},
Data.RAW_CONTACT_ID + "=" + contactid + " AND " + Data.MIMETYPE + "= '" + mimetype
+ "'", null, null);
}
Usage is
objContact.setFormality(true).saveFormality();
To add custom field you need to add custom mimetype in MIMETYPE table. But we dont have direct access to MIMETYPE table. so following can be done:
public static final String MIMETYPE="vnd.android.cursor.item/favsong";
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, id);
values.put(Data.MIMETYPE, MIMETYPE);
values.put(Data.DATA1, "MyFavSong");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
what we have done is , we have created a custom MIMETYPE as string constant.
Then using insert query we are inserting a new row in Data table having RAW_CONTACT_ID of a person we want to associate our custom field , in MIMETYPE column we put our own mimetype and in DATA1 column we put favourite song . here system internally add the new mimetype in MIMETYPE table and give it an ID and that ID is used in mimetype_id column of Data table.