0

我有 2 个按钮,它们分别调用 startActivityForResult 和 onActivityResult。第一个是联系人选择器,第二个是图片选择器。第一个操作正确,并按应有的方式将适当的联系电话返回给我的 eddittext。第二个按应有的方式开始,并允许我从我的画廊中选择一张图片,但不会按应有的方式将图片返回到我的 imagview。我认为它试图从第一个返回数据并且无法弄清楚如何区分第一个和第二个。我是 android 新手,仍在学习,犯错误,如果我在哪里以及如何出错,我将不胜感激。我有问题的代码如下。

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

2 回答 2

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

尝试使用类似上面的东西

于 2012-09-22T13:40:07.877 回答
0

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

于 2012-09-22T14:19:28.807 回答