2012-09-22 2 views
0

startActivityForResult와 onActivityResult를 각각 호출하는 두 개의 버튼이 있습니다. 첫 번째는 연락처 선택 도구이고 두 번째는 그림 선택 도구입니다. 첫 번째는 제대로 작동하고 필요한 연락처 번호를 내 eddittext에 반환합니다. 두 번째는 시작해야하며 내 갤러리에서 사진을 선택할 수는 있지만 사진을 내 imagview로 돌려 보내지는 않습니다. 나는 그것의 첫번째 것에서 자료를 돌려 보내려고 노력하고 두 번째와 첫 번째를 구별하는 방법을 이해할 수 없다고 생각한다. 나는 안드로이드 입문, 아직 배우기, 실수하기, 그리고 내가 어디서 어떻게 잘못되었는지에 대한 도움을 주시면 감사하겠습니다. 문제의 코드는 다음과 같습니다.2 startActivityForResult 및 2 onActivityResult, 두 번째 하나가 올바르게 시작하지만 첫 번째 것들 데이터를 반환하려고?

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

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

과에 startActivityForResult를 전달되는보기에 getId 전화, 당신의 OnClickListener를 위

+0

Hmm could not는 switch 문을 제대로 작동시키지 못했다.하지만 (reqCode == 1) {do stuff}와 if (reqCode == 2) {do stuff}를 시도했지만 여전히 그것을 얻을 수는 없다. 선택한 사진을 반환합니다. – user1409172

+0

예 if 문도 작동합니다. –

+0

왜 갤러리에서 사진을 반환하지 않는 이유는 무엇입니까? – user1409172

0

같은 것을 사용하여 시도하고 전화를 아래와 같이 하나의 onActivityResult를()를 사용하여 시도 버튼의 ID에 따라 다른 요청 코드. 그런 다음 onActivityResult에서받은 요청 코드에 따라 전환하십시오.

+0

예를 들어 주시겠습니까? 내가 그런 식으로 쉽게 물건을 데리러 경향이 있습니다. – user1409172

관련 문제