2013-03-05 2 views
0

작업 표시 줄 항목을 클릭하면 카메라 활동이 시작되고 사진을 캡처하고 저장 한 후 응용 프로그램을 생성하여 주 화면에 표시합니다 활동.인물 모드에서 촬영 한 이미지는 안드로이드에서 가로 모드로 유지해야합니다.

이제 MainActivity에서 카메라를 세로 방향으로 시작하고 Camera Activity에서 가로 방향으로 전환 한 다음 사진을 찍고 저장 버튼을 누르면 MainActivity로 돌아가지만 이미지가 표시되지 않습니다 , 이것은 주 활동이 재 장전되고 있기 때문에 나는 짐작한다. 그러나 나는 확실하지 않다.

방향을 변경 한 후에도 어떻게 이미지를 유지해야합니까?

MainActivity.java

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_camera: 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     // file creation for saving video 
     Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
     if (fileUri != null) { 
      targetFile = fileUri.getPath(); 

      // setting file image name 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 

      startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
     } 

     return true; 

하여 onActivityResult()

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      // Image captured and saved to fileUri specified in the Intent 
      ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture); 
      if (targetFile != null) { 
       theFile = targetFile; 
       imgPicture.setImageBitmap(BitmapFactory.decodeFile(theFile)); 
      } 

     } else if (resultCode == RESULT_CANCELED) { 

     } else { 

      Toast.makeText(this, "Your picture could not be saved.", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

Manifest.xml

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="17" /> 

<uses-feature 
    android:name="android.hardware.camera" 
    android:required="true" /> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.ActionBarTest.MainActivity" 
     android:label="@string/app_name" 
     android:launchMode="singleTop"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
+0

을 수행합니다을 onActivityResult() 메서드가 있습니까? – JRowan

+0

질문을 업데이트하고 onActivityResult() 메소드를 추가했습니다. 감사 ! – user2112791

+0

또한 실제 이미지의 크기는 메모리를 많이 먹습니다. – JRowan

답변

0

어쩌면 당신은 이미지를 저장 한 다음 방법을 알 수 있도록이를 사용할 수 있습니다 비트 맵을 만들거나 그에 기초한 것을 만드십시오.

public static int getExifRotation(String imgPath) 
    { 
     try 
     { 
      ExifInterface exif = new ExifInterface(imgPath); 
      String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
      if (!TextUtils.isEmpty(rotationAmount)) 
      { 
       int rotationParam = Integer.parseInt(rotationAmount); 
       switch (rotationParam) 
       { 
        case ExifInterface.ORIENTATION_NORMAL: 
         return 0; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         return 90; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         return 180; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         return 270; 
        default: 
         return 0; 
       } 
      } 
      else 
      { 
       return 0; 
      } 
     } 
     catch (Exception ex) 
     { 
      return 0; 
     } 
    } 

도 확인 폭이 높이 보다 클 수 및 회전 매트릭스를 취하는 비트 맵 생성자 사용 때문에 사진이 가로 인 경우 사용자에게 알려줍니다

if(bitmap.getWidth() > bitmap.getHeight()){} 

:

Matrix matrix = new Matrix(); 
matrix.postRotate(90); //or whatever