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

카메라 실행중 뒤로가기버튼 오류문제ㅠ

0 추천

안드로이드 초보자입니다.ㅠㅠ앱만드는 과정중에서 카메라로 찍어서 이미지뷰에 띄어 확인하는것을  만드는데 카메라 실행중(기본앱카메라) 백버튼하면 오류가 뜹니다. 찾아서 해봐도 해결하기 힘드네요 고수님들 조언좀 부탁드립니다ㅠㅠㅠ

 

우선로고

 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.hionshop.hionevent.CameraActivity.rotate(CameraActivity.java:168)
        at com.hionshop.hionevent.CameraActivity.onActivityResult(CameraActivity.java:195)
        at android.app.Activity.dispatchActivityResult(Activity.java:7539)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4485)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4532) 
        at android.app.ActivityThread.-wrap20(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
 

CameraActivity.java:168

}

private Bitmap rotate(Bitmap bitmap, float degree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}//밑줄부분이 168
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == 0) {
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
        ExifInterface exif = null;

        try {
            exif = new ExifInterface(mCurrentPhotoPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int exifOrientation;
        int exifDegree;

        if (exif != null) {
            exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            exifDegree = exifOrientationToDegrees(exifOrientation);
        } else {
            exifDegree = 0;
        }

        ((ImageView)findViewById(R.id.cameraImageview)).setImageBitmap(rotate(bitmap, exifDegree));
    }밑줄 부분이 195

 

익명사용자 님이 2018년 5월 24일 질문

1개의 답변

0 추천

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath); 의 반환값인 bitmap 객체가 null이 나와서,

로그에 나오듯 bitmap.getWidth() 호출 부분에서 NullPoinerException이 발생했습니다.

 

BitmapFactory.decodeFile 의 경우 아래와 같이 설명 되어 있습니다.

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

mCurrentPhotoPath 이 null 혹은 디코딩 불가능한 이미지라 Exception이 난듯 하니, mCurrentPhotoPath 값을 확인 해 보시고, bitmap 객체를 못 구할 때의 처리를 추가 해 주셔야 할 듯 합니다.

익명사용자 님이 2018년 5월 24일 답변
...