`

android 点滴积累

 
阅读更多

1、android程序查詢包含"*" "#"的號碼時出錯的解決辦法

代碼錯誤提示:
E/AndroidRuntime(1145): Caused by: android.database.sqlite.SQLiteException: near "*": syntax error: , while compiling: SELECT numbertype, new, duration, _id, numberlabel, name, number, type, date FROM calls WHERE (number = **9) ORDER BY date desc

解決: 


	strQuery = "number = " + strQueryNumber;
			callLogCursor = TApplication.nowTApplication.getContentResolver()
					.query(CallLog.Calls.CONTENT_URI, null, strQuery, null,
							"date desc");
							
							查詢代碼改成:
							
								strQuery = "number = " + "'strQueryNumber'";
			callLogCursor = TApplication.nowTApplication.getContentResolver()
					.query(CallLog.Calls.CONTENT_URI, null, strQuery, null,
							"date desc");
							
	總結:因為"*"為數據庫里的關鍵字(未經證實),所有查找是會報語法錯誤,只需要拼接字符串,把"*"改成非關鍵字即可

2、android通过查询电话号码获取联系人头像

// 取得Intent中的頭像
			ivShowImage = (ImageView) findViewById(R.id.call_log_detail_contact_img);
			//通话电话号码获取头像uri
			Uri uriNumber2Contacts = Uri
					.parse("content://com.android.contacts/"
							+ "data/phones/filter/" + strPhoneNumber);
			Cursor cursorCantacts = SApplication.nowSApplication
					.getContentResolver().query(uriNumber2Contacts, null, null,
							null, null);
			if (cursorCantacts.getCount() > 0) {	//若游标不为0则说明有头像,游标指向第一条记录
				cursorCantacts.moveToFirst();
				Long contactID = cursorCantacts.getLong(cursorCantacts
						.getColumnIndex("contact_id"));
				Uri uri = ContentUris.withAppendedId(
						ContactsContract.Contacts.CONTENT_URI, contactID);
				InputStream input = ContactsContract.Contacts
						.openContactPhotoInputStream(
								SApplication.nowSApplication
										.getContentResolver(), uri);
				btContactImage = BitmapFactory.decodeStream(input);
				Log.i("info", "bt======" + btContactImage);
				ivShowImage.setImageBitmap(btContactImage);

			} else {//么有头像设置默认头像
				ivShowImage
						.setImageResource(R.drawable.call_log_contact_image2);

			}
		



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics