2012-09-22 11 views
0

我有兩個按鈕,分別調用startActivityForResult和onActivityResult。第一個是聯繫人選擇器,第二個是圖片選擇器。第一個工作正常,並返回適當的聯繫電話號碼到我的eddittext,因爲它應該。第二個開始,因爲它應該,並允許我從我的畫廊中選擇一張照片,但不會將照片恢復到我的形象,因爲它應該。我認爲它試圖從第一個數據中返回數據,但不知道如何區分第一個和第二個數據。我是新來的機器人,仍然學習,犯錯誤,以及任何幫助,以瞭解我出錯的地方和方式,將不勝感激。我的代碼在下面。我有2 startActivityForResult和2 onActivityResult,第二個啓動正確,但trys返回第一個數據?

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

回答

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

與嘗試使用類似上述

+0

嗯不能讓那個開關語句正常工作,但是你給我一個想法嘗試if(reqCode == 1){do stuff}和else if(reqCode == 2){do stuff}但仍然不能得到它返回所選圖片。 – user1409172

+0

是的,即使是一個if語句的作品 –

+0

任何想法,爲什麼它仍然不會從畫廊tho返回圖片? – user1409172

0

在你onClickListener,調用的getId上傳遞的視圖,並調用startActivityForResult不同的請求代碼,取決於按鈕的ID。然後在您的onActivityResult中,根據收到的請求代碼進行切換。

+0

你可以舉個例子嗎?我傾向於以這種方式更容易地挑選事物。 – user1409172

相關問題