繁体   English   中英

Android-onActivityResult-无效的列_data

[英]Android - onActivityResult - Invalid column _data

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

我的问题是,如果仅在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代码

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);
            }

    }
}

我该如何解决?

我实际上解决了..

我只是忘了break; :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM