我有2个startActivityForResult和2个onActivityResult,第二个正确启动但是尝试返回第一个数据?

时间:2012-09-22 13:35:00

标签: android android-intent android-activity

我有2个按钮,它们分别调用startActivityForResult和onActivityResult。第一个是联系人选择器,第二个是图片选择器。第一个操作正确,并将适当的联系号码返回到我的eddittext。第二个开始它应该允许我从我的画廊中选择一张图片,但不会将图片返回到我的想象中。我认为它试图从第一个返回数据,并且无法弄清楚如何区分第一个和第二个。我是新手,还在学习,犯错误,以及任何有关我出错的地方和方式的帮助都会受到赞赏。我的问题代码如下。

Button.OnClickListener buttonClickListener3 = new Button.OnClickListener() {
    public void onClick(View contact) {
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 1);
        }};

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

          if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cur = managedQuery(contactData, null, null, null, null);
                ContentResolver contect_resolver = getContentResolver();

                if (cur.moveToFirst()) {
                    String id = cur.getString(cur.getColumnIndexOrThrow(BaseColumns._ID));
                    String name = "";
                    String no = "";

                    Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                    if (phoneCur.moveToFirst()) {
                        name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }

                    Log.e("Phone no & name :***: ", name + " : " + no);
                    txt.append(name + " : " + no + "/n");

                    id = null;
                    name = null;
                    no = null;
                    phoneCur = null;
                }
                contect_resolver = null;
                cur = null;

            }
    }


    Button.OnClickListener buttonClickListener4 = new Button.OnClickListener() {
      public void onClick(View ChoosePictureButton) {
          Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
          startActivityForResult(choosePictureIntent, 2);


        }};


        public void onActivityResult2(int requestCode2, int resultCode2, Intent intent) {
          super.onActivityResult(requestCode2, resultCode2, intent);


          if (resultCode2 == RESULT_OK) {
            Uri imageFileUri = intent.getData();

            try {
              BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
              bmpFactoryOptions.inJustDecodeBounds = true;
              Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
              bmpFactoryOptions.inSampleSize = 2;
              bmpFactoryOptions.inJustDecodeBounds = false;
              bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                      imageFileUri), null, bmpFactoryOptions);

              ChosenImageView.setImageBitmap(bmp);
            } catch (FileNotFoundException e) {
              Log.v("ERROR", e.toString());
            }
          }
        }

2 个答案:

答案 0 :(得分:1)

尝试使用单个onActivityResult(),如下所示

public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);

      if (resultCode == Activity.RESULT_OK) {
switch(reqCode)
{
 case 1:
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(BaseColumns._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                txt.append(name + " : " + no + "/n");

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;

        }
}
break ;

case 2:
        Uri imageFileUri = intent.getData();

        try {
          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
          bmpFactoryOptions.inJustDecodeBounds = true;
              Bitmap bmp =  
   BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,  
 bmpFactoryOptions);
          bmpFactoryOptions.inSampleSize = 2;
          bmpFactoryOptions.inJustDecodeBounds = false;
          bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                  imageFileUri), null, bmpFactoryOptions);

          ChosenImageView.setImageBitmap(bmp);
        } catch (FileNotFoundException e) {
          Log.v("ERROR", e.toString());
        }
      }
    }
}

尝试使用上述内容

答案 1 :(得分:0)

在onClickListener中,在传入的视图上调用getId,并使用不同的请求代码调用startActivityForResult,具体取决于按钮的ID。然后在你的onActivityResult中,根据它收到的请求代码进行切换。