2014-04-08 4 views
2

사진을 찍어 ImageView에 넣으려고합니다. 코드가 onActivityResult에 도달하면 Intent는 항상 null입니다. 이 코드는 내가 조사한 한 제대로 된 것 같지만 작동시키지는 못한다.사진을 찍을 때 의도는 항상 null입니다.

@Override 
public void onClick(View v) { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && data != null) { 
     Bundle extras = data.getExtras(); 
     Bitmap bmap = (Bitmap) extras.get("data"); 
     switchImage(bmap); 
    } 
} 
+1

스택 추적을 게시하십시오. –

+0

오류 로그를 게시하십시오. –

+0

내 대답을 확인하십시오 – Lunchbox

답변

0

다음 코드를 사용하여 사진을 찍을 수 있습니다. 캡쳐 버튼을

클릭 : onActivityResult를에서

private void cameraCapture() { 
     File photoFile = null; 
     if (pic_number >= 5) { 
      Toast.makeText(getApplicationContext(), "Maximum amount of pictures taken", Toast.LENGTH_LONG).show(); 
     } else { 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "File not created", Toast.LENGTH_LONG).show(); 
      } 
      if (photoFile != null) { 
       Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       camera_picture_uri = photoFile.toString(); 
       camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
       startActivityForResult(camIntent, pic_number); 
       pic_number++; 
      } 
     } 
    } 


private File createImageFile() throws IOException { 
     String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = getApplicationContext().getExternalFilesDir(null); 
     File image = File.createTempFile(imageFileName, ".jpg", storageDir); 
     return image; 

    } 

:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     setCameraPicture(data); 
    } 

private void setCameraPicture(Intent data) { 
     // TODO 
     // Auto-generated method stub 
     Bitmap bm = null; 
     try { 
      File file = new File(camera_picture_uri); 
      ExifInterface ei = new ExifInterface(camera_picture_uri); 
      int rotation = 0; 
      int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

      if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
       rotation = 90; 
       bm = rotateCameraImage(rotation, file); 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
       rotation = 180; 
       bm = rotateCameraImage(rotation, file); 
      } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
       rotation = 270; 
       bm = rotateCameraImage(rotation, file); 
      } else { 
       bm = rotateCameraImage(rotation, file); 
      } 

      // Set bitmap to imageview 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (NullPointerException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 


private Bitmap rotateCameraImage(int rotation, File f) throws FileNotFoundException { 
     // TODO Auto-generated method stub 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(rotation); 

     BitmapFactory.Options o = new BitmapFactory.Options(); 
     int scale = 8; 
     o.inSampleSize = scale; 
     o.inPurgeable = true; 
     o.inInputShareable = true; 

     Bitmap sourceBitmap = BitmapFactory.decodeFile(f.toString(), o); 
     Bitmap bitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true); 
     System.gc(); 
     return bitmap; 
    } 

편집

ExIfInterface를 사용하는이 사용자 정의 ROM을뿐만 아니라로드 한 삼성 기기에서 작동 올바르게 표시하기 위해 이미지 회전 설정을 확인하려면

+0

널 의도 상 문제점이 있었는데이 문제가 해결되었습니다. – Lunchbox

관련 문제