2012-09-11 124 views
1

當我從圖庫中選擇圖像並在ImageView中顯示圖像時,某些圖像會自動旋轉90度。禁用Android圖像自動旋轉

如何禁用此功能?

代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main); 

    m_galleryIntent = new Intent(); 
    m_galleryIntent.setType("image/*"); 
    m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 

    m_ProfileImageView = (ImageView) findViewById(R.id.imageView1); 

    m_ProfileImageView.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);       
     } 

    }); 
} 


public Bitmap readBitmap(Uri selectedImage) 
{ 
    Bitmap bm = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 5; 
    AssetFileDescriptor fileDescriptor =null; 
    try 
    { 
     fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r"); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     try 
     { 
      bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options); 
      fileDescriptor.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    return bm; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) 
    { 
     if (requestCode == 1) 
     { 

      try 
      { 
       Uri imageURI = data.getData(); 
       bitmapFromFile = readBitmap(imageURI);   
       m_ProfileImageView.setImageBitmap(bitmapFromFile); 

      } 
      catch (Exception e) 
      {       
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

我認爲你改變你的設備方向。肖像風景。 在您的活動中使用android:screenOrientation =「portrait」以防止您的活動進入橫向模式 –

+0

我不改變方向。我試過了,這不是問題。 但是,謝謝。 – Rami

回答

4

你應該自己轉動這個圖像。從內容提供者讀取圖像方向值,特別是Images.Media.ORIENTATION字段並相應地旋轉它。

該圖像存儲旋轉。旋轉角度保存在媒體數據庫中。

public int getOrientation(Uri selectedImage) { 
    int orientation = 0; 
    final String[] projection = new String[]{MediaStore.Images.Media.ORIENTATION};  
    final Cursor cursor = context.getContentResolver().query(selectedImage, projection, null, null, null); 
    if(cursor != null) { 
     final int orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION); 
     if(cursor.moveToFirst()) { 
      orientation = cursor.isNull(orientationColumnIndex) ? 0 : cursor.getInt(orientationColumnIndex); 
     } 
     cursor.close(); 
    } 
    return orientation; 
} 

例如,您可以使用ImageView.setImageMatrix()來旋轉圖像。