开发者

Intent.getData() is null when trying to read contact data on device

开发者 https://www.devze.com 2023-01-14 07:28 出处:网络
I am writting my application using api 1.6. The code runs just fine on the emulators 1.6-2.2. But when I try to debugging it on my device which runs 2.1 data.getData() returns null. I have this in t

I am writting my application using api 1.6. The code runs just fine on the emulators 1.6-2.2.

But when I try to debugging it on my device which runs 2.1 data.getData() returns null. I have this in the AndroidManifest.xml <uses-permission android:name="android.permission.READ_CONTACTS"/>

Any one have any thoughts. I am new to android开发者_运维技巧, thanks.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnTest = (Button) findViewById(R.id.btnTest);
        btnTest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);

                startActivityForResult(intent, ACTIVITY_PICK_CONTACT);
            }
        });
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case (ACTIVITY_PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                // This is where it is broke
                            Uri uri = data.getData();

                Intent intent = new Intent(this, PickContactInfo.class);
                intent.putExtra(Keys.CONTACT_URI, uri.toString());
                startActivity(intent);
            }
            break;
        }

    };


There are a few possibilities.

First, try switching from People.CONTENT_URI to Contacts.CONTENT_URI. I have not had a problem with that returning a null contact. Here is a sample project.

Second, make sure that the contact you are picking is one that is on the device itself (vs. one synced to it from Facebook, Exchange, etc.). The old API only works with on-device contacts.

If none of that helps, there may be a bug with the firmware in your device. I'd be interested to know, in this case, what device this is.

0

精彩评论

暂无评论...
验证码 换一张
取 消