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

앱에서 사진 촬영후 이미지 출력시 URL 에러 문의

0 추천

앱에서 사진 촬영을 한후 이미지뷰에서 촬영한 사진을 출력할려고하니 

URL NULL 포인트가 발생합니다.

갤러리에서 가져와서는 출력이 되는데....

 

수정방안 알려주세요.

 

  public void select_picture(String rec_string){
      switch (rec_string){
          case "0":
              Intent takeapicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(takeapicture, CAMERA_CODE);
              break;
          case "1":
              Intent take_picture = new Intent(Intent.ACTION_PICK);
              take_picture.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
              take_picture.setType("image/*");
              startActivityForResult(Intent.createChooser(take_picture, "Select Picture"), GALLERY_CODE);
              break;
          case "2":
              image.setVisibility(View.VISIBLE);
              Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.noimage);
              image.setImageBitmap(bm);//이미지 뷰에 비트맵 넣기
              break;
      }
    }

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case GALLERY_CODE:
                    SendPicture(data); //갤러리에서 가져오기
                    break;
                case CAMERA_CODE:
                    SendPicture(data); //카메라에서 가져오기
                    break;
                default:
                    break;
            }
        }
    }

    private void SendPicture(Intent data) {
        Uri imgUri = data.getData();
        String imagePath = getRealPathFromURI(imgUri); // path 경로
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int exifDegree = exifOrientationToDegrees(exifOrientation);

        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);//경로를 통해 비트맵으로 전환
        image.setVisibility(View.VISIBLE);
        image.setImageBitmap(rotate(bitmap, exifDegree));//이미지 뷰에 비트맵 넣기

    }
    public int exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }

    public Bitmap rotate(Bitmap src, float degree) {

        // Matrix 객체 생성
        Matrix matrix = new Matrix();
        // 회전 각도 셋팅
        matrix.postRotate(degree);
        // 이미지와 Matrix 를 셋팅해서 Bitmap 객체 생성
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(),
                src.getHeight(), matrix, true);
    }

    public String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);

    }

 

쿠쿠부다스 (6,470 포인트) 님이 2017년 10월 2일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...