Reputation: 139
I'm trying to get a code working to select a contact and retrieve the phone number. I found a piece of code which works great on the Emulator (android version 2.3.3) and on my brother's phone (Jelly bean) but not my on own device (android 2.3.4)
Here's the code: To go to contacts
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
When coming back from contacts:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
if (c != null && c.moveToFirst()) {
String number = c.getString(0);
//int type = c.getInt(1);
showSelectedNumber(0, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void showSelectedNumber(int type, String number) {
Toast.makeText(this,number, Toast.LENGTH_LONG).show();
}
Again, this works on the emulator (android 2.3.4) and on Jelly bean but not on my device (android 2.3.5)
This is what comes out from the log:
10-10 22:07:15.775: E/AndroidRuntime(5075): FATAL EXCEPTION: main
10-10 22:07:15.775: E/AndroidRuntime(5075): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/2848 (has extras) }} to activity {com.example.omer/com.example.omer.SmsInfo}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/2848 from pid=5075, uid=10125 requires android.permission.READ_CONTACTS
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.deliverResults(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.handleSendResult(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.access$2000(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Handler.dispatchMessage(Handler.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Looper.loop(Looper.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.ActivityThread.main(ActivityThread.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 22:07:15.775: E/AndroidRuntime(5075): at java.lang.reflect.Method.invoke(Method.java:507)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at dalvik.system.NativeStart.main(Native Method)
10-10 22:07:15.775: E/AndroidRuntime(5075): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/2848 from pid=5075, uid=10125 requires android.permission.READ_CONTACTS
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.os.Parcel.readException(Parcel.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentProviderProxy.query(ContentProviderNative.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.content.ContentResolver.query(ContentResolver.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): at com.example.omer.SmsInfo.onActivityResult(SmsInfo.java:87)
10-10 22:07:15.775: E/AndroidRuntime(5075): at android.app.Activity.dispatchActivityResult(Activity.java)
10-10 22:07:15.775: E/AndroidRuntime(5075): ... 12 more
Upvotes: 0
Views: 665
Reputation: 139
Ok.. just got it I had to add a permission to the manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Upvotes: 1
Reputation: 137422
According to the error message:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/2848 (has extras) }} to activity {com.example.omer/com.example.omer.SmsInfo}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/2848 from pid=5075, uid=10125 requires android.permission.READ_CONTACTS
You need to add the permission android.permission.READ_CONTACTS
Upvotes: 0