본문 바로가기

Develop/Android SDK

카메라 or 갤러리 호출 하기


@Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {

   final String items[] = {"갤러리에서 가져오기","카메라로 촬영하기"};
   if (url.startsWith("custom://")) {
      new AlertDialog.Builder(MyPageWeb.this)
         .setIcon(R.drawable.icon)
         .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int item) {
             Toast.makeText(getApplicationContext(), Integer.toString(item), Toast.LENGTH_SHORT).show();
             dialog.dismiss();
             
             if(item==0) {
//갤러리 호출
              Uri uri = Uri.parse("content://media/external/images/media");
                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                 intent.setAction(Intent.ACTION_GET_CONTENT);
                 intent.setType("image/*");
                 startActivityForResult(intent, REQ_SELECT);

             } else if(item==1) {
//카메라로 찍기
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 startActivity(intent);
             }
         }
         })

        .show();
   }
}



//////////////////////////// 선택 하면 리턴값 받기
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
   super.onActivityResult(requestCode, resultCode, intent);
    try {
     if(!intent.getData().equals(null)){

      Bitmap selPhoto = Images.Media.getBitmap(getContentResolver(), intent.getData());

     selPhoto = Bitmap.createScaledBitmap(selPhoto, 100, 100, true);

//      image_bt.setImageBitmap(selPhoto);//썸네일
      Log.e("선택 된 이미지 ", "selPhoto : " + selPhoto);
     }
    
    } catch (FileNotFoundException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }

 }