Android - onActivityResult - 无效的列_data

时间:2015-03-30 15:59:03

标签: java android mobile onactivityresult

我正在尝试发送短信和彩信的应用程序,因此我需要检索电话号码并最终检索图像。

我的问题是,如果我只是 onActivityResult 中的 PICK_CONTACT 案例,它工作正常,我收到了我的联系电话号码。
但如果我添加第二部分 PICK_IMAGE ,当我点击我的联系人时,我得到了:

java.lang.IllegalArgumentException: Invalid column _data

但是我仍然可以毫无问题地拍摄图像。

两个意图呼叫

private static final int PICK_CONTACT = 3;
private static final int PICK_IMAGE   = 4;

protected void onCreate(Bundle savedInstanceState) {
    ...
}

public void addImage() {
    Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(choosePictureIntent,PICK_IMAGE );
}
public void addContact(){
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
}

onActivityResult Code

public void onActivityResult(int reqCode, int resultCode, Intent data){
    super.onActivityResult(reqCode, resultCode, data);
    Uri contactData = data.getData();

    switch(reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

                if (cur.moveToFirst()) {

                    String id   = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                    while (pCur.moveToNext()) {
                        phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        etPhoneNo.setText(phone);
                    }
                    pCur.close();
                }
            }
        case (PICK_IMAGE) :
            if (resultCode == Activity.RESULT_OK) {
                try {
                    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                    bmpFactoryOptions.inJustDecodeBounds = true;
                    bmpFactoryOptions.inSampleSize = 2;
                    bmpFactoryOptions.inJustDecodeBounds = false;
                    Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(contactData), null, bmpFactoryOptions);
                    imageView.setImageBitmap(bmp);
                } catch (FileNotFoundException e) {
                    Log.v("ERROR", e.toString());
                }

                ContentResolver cr = getContentResolver();
                String [] proj = {MediaStore.Images.Media.DATA};
                Cursor cursor = cr.query(contactData, proj, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                image_path = cursor.getString(column_index);
            }

    }
}

我该如何解决?

0 个答案:

没有答案