2013-03-05 101 views
0

我正在創建一個應用程序,在點擊動作欄項目時,照相機活動開始,並且在拍攝並保存照片後,將其顯示在主活動。在肖像模式下拍攝的圖片應該保留在橫向模式下android

現在,如果我在縱向方向上從MainActivity啓動相機,並在相機活動中切換到橫向,拍攝照片並單擊保存按鈕,它將返回到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 2013-03-05 21:39:56

+0

我已經更新了問題並添加了onActivityResult()方法。謝謝 ! – user2112791 2013-03-05 21:48:43

+0

也是實際圖像的大小吃了很多內存,你可能會考慮縮小它 – JRowan 2013-03-05 21:57:25

回答

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 
相關問題