從ContactPicker我得到的接觸從各種設備的聯繫人,但在試圖獲取從HTC設備我流汗以下異常,請聯繫:無法獲得HTC設備
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/719 from pid=8344, uid=10214 requires android.permission.READ_CONTACTS, or grantUriPermission()
權限,我有給出的是:
android.permission.READ_CONTACTS
下面是我的實現代碼:
Intent intent = new Intent(Intent.ACTION_PICK,
Phone.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 101);
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
if (arg1 == Activity.RESULT_OK) {
Uri contactData = arg2.getData();
ContentResolver cur = getContentResolver();
Cursor cursorID = getContentResolver().query(contactData,
new String[] { ContactsContract.Contacts._ID }, null, null,
null);
grantUriPermission("ContactsContract.CommonDataKinds.Phone.CONTENT_URI", contactData, 2);
Cursor c = cur.query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c
.getString(c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = c
.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(),
"" + name + " " + number, 4000).show();
}
}
}
任何幫助表示讚賞。
這裏是我的Android清單文件: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.readconatc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
「ContactsContract.CommonDataKinds.Phone.CONTENT_URI」<=打字錯誤? ...爲什麼有撇號? ...你在哪裏放置了「android.permission.READ_CONTACTS」權限?在AndroidManifest的應用程序節點中? – Selvin
請將您的'AndroidManifest.xml' –
清單文件代碼添加。 – AmmY