1

When I'am selecting image from gallery and showing the image in ImageView some of the images is auto rotated with 90 degrees.

How do I disable this?

Code:

@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();
            } 
        }
    }
}
Rami
  • 2,098
  • 5
  • 25
  • 37
  • I think you change your device orienation . Portrait to Landscape. Use android:screenOrientation="portrait" in your activity to prevent your activity to landscape mode – Chirag Sep 11 '12 at 11:59
  • I'am not changing the orientation. I tried it, it is not the problem. But thanks. – Rami Sep 11 '12 at 12:00

1 Answers1

4

You should rotate this images yourself. Read image orientation value from content provider, specifically Images.Media.ORIENTATION field and rotate it correspondingly.

This images are stored rotated. Rotate angle is saved in media database.

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;
}

You can rotate image using ImageView.setImageMatrix() for example.

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56