2011-10-10 5 views
0

안녕하세요 저는이 버튼을 클릭하고 카메라 & 갤러리 중 하나를 선택할 수 있도록 Complete Action Using 창을 시작하고 싶습니다.Android 완료 액션을 사용하여

대화 상자를 만드는 것 외에도 이것을 구현하는 쉬운 방법이 있습니까?

답변

2
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(
        context.getString(R.string.Select_an_Option_to_add_Photo)) 
        .setCancelable(true) 
        .setPositiveButton(context.getString(R.string.Camera), 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            try { 
             Intent action = new Intent(
               "android.media.action.IMAGE_CAPTURE"); 
             action.putExtra(
               MediaStore.EXTRA_OUTPUT, 
               MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
                 .toString()); 
             startActivityForResult(action, 8); 
            } catch (Exception e) { 
             // TODO Auto-generated catch block 
             e.printStackTrace(); 
            } 
           } 

          }) 
        .setNegativeButton(context.getString(R.string.Gallery), 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            dialog.cancel(); 

            try { 
             Intent photoPickerIntent = new Intent(
               Intent.ACTION_GET_CONTENT); 
             photoPickerIntent.setType("image/*"); 
             startActivityForResult(photoPickerIntent, 1); 
            } catch (Exception e) { 
             // TODO Auto-generated catch block 
             e.printStackTrace(); 
            } 
           } 
          }); 
      alert = builder.create(); 

이제

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == RESULT_OK && requestCode == 8) { 

      Bitmap photoBitMap = (Bitmap) data.getExtras().get("data"); 

      Bitmap usableBMP = Bitmap.createScaledBitmap(photoBitMap, 68, 80, 
        true); 
//This is my ImageView Object   
cameraButton.setImageBitmap(usableBMP); 
      cameraButton.setScaleType(ScaleType.CENTER_INSIDE); 
     } else if (resultCode == RESULT_OK) { 
      Uri chosenImageUri = data.getData(); 
      try { 
//Here I scale my Bitmap as desired 
       photoBitMap = Media.getBitmap(this.getContentResolver(), 
         chosenImageUri); 
       Bitmap usableBMP = Bitmap.createScaledBitmap(photoBitMap, 68, 
         80, true); 

//this is my ImageView Object 
       cameraButton.setImageBitmap(usableBMP); 
       cameraButton.setScaleType(ScaleType.CENTER_INSIDE); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 
+0

이 쉽게 솔루션을 원하는 –

+0

감사 Arpit을 달성 할 수있는 또 다른 방법입니다! – lemon

관련 문제