마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

(안드로이드) 카메라 앨범에서 데이터 불러오기 [closed]

0 추천

안녕하세요!

카메라를 켜서 찍고, 크랍하고, 저장하는 것을 연습하고 있습니다.

카메라를 먼저 작동 시키고 저장하는 것은 문제가 없으나, 카메라를 작동하지 않고 앨범에서 사진을 불러와 작업후 저장하는데 문제가 있습니다..

소스는 아래처럼 작성했습니다.

 

 

...

Uri photoURI = null;

...

 

기본적인 사진찍기는 아래처럼


    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile(); // 사진찍은 후 저장할 임시 파일(껍데기)
            } catch (IOException ex) {
                Toast.makeText(getApplicationContext(), "createImageFile Failed", Toast.LENGTH_LONG).show();
            }

            if (photoFile != null) {
                Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE ); // 동기화
                photoURI = Uri.fromFile(photoFile); // 임시 파일의 위치,경로 가져옴
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); // 임시 파일 위치에 저장
                mediaScanIntent.setData(photoURI); // 동기화
                this.sendBroadcast(mediaScanIntent); // 동기화
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

 

 

임시폴더 생성하기


    private File createImageFile() throws IOException {
        // Create an image file name
        String imageFileName = "tmp_" + String.valueOf(System.currentTimeMillis());

        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MYAPP/");


        File file = File.createTempFile(imageFileName, ".jpg", storageDir);

        return file;
    }

 

이건 앨범에서 가져오기


    private void doTakeAlbumAction()
    {
        // 앨범 호출
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
    }

 

 

그리고 RESULT값에 따른 호출

 

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

        if (resultCode != RESULT_OK) {
            Toast.makeText(getApplicationContext(), "onActivityResult : RESULT_NOT_OK", Toast.LENGTH_LONG).show();
        }
        switch (requestCode) {

            case REQUEST_TAKE_PHOTO: // 앨범이미지 가져오기
                photoURI = data.getData();
                // break; 바로 cropImage()로 전달되도록
            case REQUEST_IMAGE_CAPTURE: 
                cropImage();

                break;
            case REQUEST_IMAGE_CROP:

                Bitmap photo = BitmapFactory.decodeFile(photoURI.getPath());
                iv_capture.setImageBitmap(photo);
                //Bitmap photo = BitmapFactory.decodeFile(mCurrentPhotoPath);
                /* photo가져올 때 옵션 지정 가능, 아래는 예
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inInputShareable = true;
                options.inDither=false;
                options.inTempStorage=new byte[32 * 1024];
                options.inPurgeable = true;
                options.inJustDecodeBounds = false;
                */

                break;

        }
    }

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

이건 크랍하기!


    private void cropImage() {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");

        cropIntent.setDataAndType(photoURI, "image/*");
        cropIntent.putExtra("scale", true);
        cropIntent.putExtra("output", photoURI); // 크랍된 이미지를 해당 경로에 저장
        startActivityForResult(cropIntent, REQUEST_IMAGE_CROP);
    }

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

 

이렇게 하고 있습니다. 

 

그런데 앨범을 켜서 사진을 가져와 Crop으로 작업한 뒤 저장버튼을 누르면 아래처럼

onActivityResult : RESULT_NOT_OK

가 호출되더라구요. 이유가 뭘까요? cropImage부분에서 이미지 저장이 안된거같은데..

data.getData() 이후에 추가적인 작업이 필요한가요..?

예제보면서 하는데 잘 안되네요 ㅠㅜ

 

** 그리고 앨범 연다음에 사진 선택안하고 뒤로가기 하면 어플이 꺼져버리던데, 왜  앱이 꺼져버릴까요.

위에 처럼 RESULT_NOT_OK만 뜨고 뒤로가기가 되어야하는데, getData()부분에서 널 값 리턴되었다고 나오더라구요.

 

답변 부탁드립니다. (__)

감사합니다 :)

 

 

질문을 종료한 이유: 답변 없음
겸군님 (1,900 포인트) 님이 2016년 9월 13일 질문
겸군님님이 2017년 2월 3일 closed
...