본문 바로가기

안드로이드

카메라 호출 후 넘어오는 intent.getData()가 null 일때 해결법

카메라 호출 한후 이미지를 저장했을때 이미지 intent uri  값이 제대로 오지 않을때가 있다. 해결법은 카메라에 이미지 저장경로를 지정해준 후 intent를 날린후 받아올때 그 경로에서 이미지를 찾아오면 잘된다.


-----------------------------------------------------------------------------------------

AlertDialog alert1=null;

Uri mImageCaptureUri=null;

Uri imgUri;

ImageView cImg;


private void selectJob(){

AlertDialog.Builder adialog = new AlertDialog.Builder(AddPhoneNumber.this);

adialog.setInverseBackgroundForced(true);

adialog.setMessage("작업을 선택하세요..!").setPositiveButton("촬영하기", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

alert1.dismiss();

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

   String url = "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg";

   mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), url));

   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

   

   startActivityForResult(intent, 0);

}

}).setNeutralButton("파일선택" , new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

alert1.dismiss();

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);

    startActivityForResult(intent, 1);  

}

}).setNegativeButton("취소", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

alert1.dismiss();

}

});

 alert1 = adialog.create();

 alert1.setTitle("작업선택");

 alert1.show();

}


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

    {

    if(resultCode == RESULT_OK){

     if(requestCode==0)  {

        imgUri= mImageCaptureUri;

        Bitmap selPhoto=null;

try {

AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(imgUri, "r");       

BitmapFactory.Options opt = new BitmapFactory.Options(); 

opt.inSampleSize = 4; 

selPhoto = BitmapFactory.decodeFileDescriptor(afd.getFileDescriptor(), null, opt);

// selPhoto = Images.Media.getBitmap( getContentResolver(), imgUri );

} catch (OutOfMemoryError e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

                Drawable drawable = new BitmapDrawable(getResources(), selPhoto);

                cImg.setBackgroundDrawable(drawable);

     }


     else  if(requestCode==1)  {

     if(data !=null){

     try {

     imgUri= data.getData();

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

                      Drawable drawable = new BitmapDrawable(getResources(), selPhoto);

                      cImg.setBackgroundDrawable(drawable);

                } catch (Exception e) {

                       e.printStackTrace();

                } 

     }

 }

    }

    }

-----------------------------------------------------------------------------------------


http://theeye.pe.kr/entry/example-of-image-crop-with-camera-and-album-picker-on-android 참고했습니다.